【问题标题】:Video goes black after few seconds after replacing AVPlayerItem更换 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];
}];

【问题讨论】:

    标签: ios objective-c avplayer avplayeritem avplayerlayer


    【解决方案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];}
    

    【讨论】:

    • 请先解决我的问题。这当然不是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 2013-08-11
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    相关资源
    最近更新 更多