MPMoviePlayerController:不播放视频
MPMoviePlayerController: Not playing video
我正在尝试使用 MPMoviePlayerController 播放本地保存的视频,但它不起作用。
这是我的来源:
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayer prepareToPlay];
CGRect frame = self.movieView.frame;
frame.origin = CGPointZero;
self.moviePlayer.view.frame = frame;
self.moviePlayer.allowsAirPlay = NO;
self.moviePlayer.shouldAutoplay = NO;
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.movieView addSubview:self.moviePlayer.view];
当我有一个像 "assets-library://asset/asset.MOV?id=2C7D89F6-2211-4920-A842-30D773B075D6&ext=MOV" 的 URL 但当我有一个像 "file:///var/mobile/Media/DCIM/101APPLE/IMG_1454.mp4" 的 URL 时,它 确实 工作不起作用。
我正在使用以下代码获取用户最新视频并在播放器中播放:
- (void)mostRecentVideo:(void (^)(NSURL *url))completion
{
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];
PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.deliveryMode = PHVideoRequestOptionsDeliveryModeMediumQualityFormat;
[[PHImageManager defaultManager] requestAVAssetForVideo:lastAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]])
completion(((AVURLAsset *)asset).URL);
}];
}
我猜你在
中对url
使用了NSUrl *url = [NSURL URLWithString:urlStr];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
但是文件路径如下:file:///var/mobile/Media/DCIM/101APPLE/IMG_1454.mp4
你必须使用 :
NSUrl *url = [NSURL fileURLWithPath:filePath];
我对您的代码做了一些更改,并将电影播放器代码放入 PHImageManager 块中,它为我工作。
[[PHImageManager defaultManager] requestAVAssetForVideo:lastAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]])
NSLog(@"%@",((AVURLAsset *)asset).URL);
AVURLAsset *assetURL = [AVURLAsset assetWithURL:((AVURLAsset *)asset).URL];
NSString *str= [NSString stringWithFormat:@"%@",assetURL.URL];
NSURL *url = [NSURL URLWithString:str];
// Initialize the MPMoviePlayerController object using url
_videoPlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[_videoPlayer.view setFrame:CGRectMake(0, 0, 320, 568)];
// Add a notification. (It will call a "moviePlayBackDidFinish" method when _videoPlayer finish or stops the plying video)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_videoPlayer];
// Set control style to default
_videoPlayer.controlStyle = MPMovieControlStyleDefault;
// Set shouldAutoplay to YES
_videoPlayer.shouldAutoplay = YES;
[_videoPlayer setMovieSourceType:MPMovieSourceTypeFile];
// Add _videoPlayer's view as subview to current view.
[self.view addSubview:_videoPlayer.view];
// Set the screen to full.
[_videoPlayer setFullscreen:YES animated:YES];
}];
请查看是否对您有帮助...
同样的代码对我有用,我是这样用的。由于获取 url 是一个异步操作,因此必须在主队列上更新 UI 。只需将代码放入 viewDidAppear
并重试。
[self mostRecentVideo:^(NSURL *url){
NSLog(@"did play!, url is %@",url);
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayer prepareToPlay];
CGRect frame = self.view.frame;
frame.origin = CGPointZero;
self.moviePlayer.view.frame = frame;
self.moviePlayer.allowsAirPlay = NO;
self.moviePlayer.shouldAutoplay = NO;
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:self.moviePlayer.view];
});
[self.moviePlayer play];
}];
我正在尝试使用 MPMoviePlayerController 播放本地保存的视频,但它不起作用。
这是我的来源:
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayer prepareToPlay];
CGRect frame = self.movieView.frame;
frame.origin = CGPointZero;
self.moviePlayer.view.frame = frame;
self.moviePlayer.allowsAirPlay = NO;
self.moviePlayer.shouldAutoplay = NO;
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.movieView addSubview:self.moviePlayer.view];
当我有一个像 "assets-library://asset/asset.MOV?id=2C7D89F6-2211-4920-A842-30D773B075D6&ext=MOV" 的 URL 但当我有一个像 "file:///var/mobile/Media/DCIM/101APPLE/IMG_1454.mp4" 的 URL 时,它 确实 工作不起作用。
我正在使用以下代码获取用户最新视频并在播放器中播放:
- (void)mostRecentVideo:(void (^)(NSURL *url))completion
{
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];
PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.deliveryMode = PHVideoRequestOptionsDeliveryModeMediumQualityFormat;
[[PHImageManager defaultManager] requestAVAssetForVideo:lastAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]])
completion(((AVURLAsset *)asset).URL);
}];
}
我猜你在
中对url
使用了NSUrl *url = [NSURL URLWithString:urlStr];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
但是文件路径如下:file:///var/mobile/Media/DCIM/101APPLE/IMG_1454.mp4
你必须使用 :
NSUrl *url = [NSURL fileURLWithPath:filePath];
我对您的代码做了一些更改,并将电影播放器代码放入 PHImageManager 块中,它为我工作。
[[PHImageManager defaultManager] requestAVAssetForVideo:lastAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]])
NSLog(@"%@",((AVURLAsset *)asset).URL);
AVURLAsset *assetURL = [AVURLAsset assetWithURL:((AVURLAsset *)asset).URL];
NSString *str= [NSString stringWithFormat:@"%@",assetURL.URL];
NSURL *url = [NSURL URLWithString:str];
// Initialize the MPMoviePlayerController object using url
_videoPlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[_videoPlayer.view setFrame:CGRectMake(0, 0, 320, 568)];
// Add a notification. (It will call a "moviePlayBackDidFinish" method when _videoPlayer finish or stops the plying video)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_videoPlayer];
// Set control style to default
_videoPlayer.controlStyle = MPMovieControlStyleDefault;
// Set shouldAutoplay to YES
_videoPlayer.shouldAutoplay = YES;
[_videoPlayer setMovieSourceType:MPMovieSourceTypeFile];
// Add _videoPlayer's view as subview to current view.
[self.view addSubview:_videoPlayer.view];
// Set the screen to full.
[_videoPlayer setFullscreen:YES animated:YES];
}];
请查看是否对您有帮助...
同样的代码对我有用,我是这样用的。由于获取 url 是一个异步操作,因此必须在主队列上更新 UI 。只需将代码放入 viewDidAppear
并重试。
[self mostRecentVideo:^(NSURL *url){
NSLog(@"did play!, url is %@",url);
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayer prepareToPlay];
CGRect frame = self.view.frame;
frame.origin = CGPointZero;
self.moviePlayer.view.frame = frame;
self.moviePlayer.allowsAirPlay = NO;
self.moviePlayer.shouldAutoplay = NO;
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:self.moviePlayer.view];
});
[self.moviePlayer play];
}];