gpt4 book ai didi

ios - MPMoviePlayerController:不播放视频

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:01:57 24 4
gpt4 key购买 nike

我正在尝试使用 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 是一个异步操作,因此必须在主队列上更新 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];
}];

关于ios - MPMoviePlayerController:不播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28956016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com