更换 AVPlayerItem 几秒钟后视频变黑

Posted

技术标签:

【中文标题】更换 AVPlayerItem 几秒钟后视频变黑【英文标题】:Video goes black after few seconds after replacing AVPlayerItem 【发布时间】:2017-09-20 09:45:26 【问题描述】:

当我第一次播放任何视频时,视频播放非常好,但是当我从手机的图库中选择一个视频并替换它时,视频在 2 秒后变黑并且音频播放到最后。 在此处添加视频播放器:

-(void) addMoviePlayer
NSString *path = [[NSBundle mainBundle] pathForResource:@"got1" ofType:@"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:path];

AVAsset *asset = [AVAsset assetWithURL:videoURL];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer *vidPlayer = [[AVPlayer alloc] initWithPlayerItem:item];
CGRect vidRect = CGRectMake(0, self.view.bounds.size.height * 0.15, self.view.bounds.size.width, self.view.bounds.size.height * 0.3);

videoLayer = [AVPlayerLayer layer];
[videoLayer setPlayer:vidPlayer];
[videoLayer setBackgroundColor:[UIColor clearColor].CGColor];
[videoLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
videoLayer.frame = vidRect;
[self.view.layer addSublayer:videoLayer];
vidPlayer.muted = true;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:vidPlayer.currentItem];

然后我将 AVPlayerItem 替换为从图库中挑选的那个。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"VideoURL = %@", videoURL);

[picker dismissViewControllerAnimated:YES completion:^
    AVPlayerItem *currentItem = [[AVPlayerItem alloc] initWithURL:videoURL];
    [videoLayer.player replaceCurrentItemWithPlayerItem:currentItem];
    [videoLayer.player play];
];

【问题讨论】:

【参考方案1】:

希望这会对你有所帮助。

-(void)playVideoFile:(NSURL *)fileUrl 

AVPlayer *videoPlayer = [[AVPlayer alloc] initWithURL:fileUrl];  
AVPlayerViewController *videoController = [AVPlayerViewController new];
videoController.player = videoPlayer;

[self presentViewController:videoController animated:YES completion:nil];

【讨论】:

请先解决我的问题。这当然不是我想要的。

以上是关于更换 AVPlayerItem 几秒钟后视频变黑的主要内容,如果未能解决你的问题,请参考以下文章

无法导出 AVPlayerItem

我希望我的关键帧动画在一段时间后重新启动

播放几秒钟的视频 onclick

Android VideoView:从父视图中删除视频后变黑

FFMPEG- 流媒体在几秒钟后停止

使用 OpenCV 仅在视频文件开头几秒钟提取帧