当我第一次播放任何视频时,视频播放效果非常好但是当我从手机画廊中选择视频并更换视频时,视频会在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替换为从图库中选取的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];
}];
答案 0 :(得分:0)
希望这会对你有所帮助。
-(void)playVideoFile:(NSURL *)fileUrl{
AVPlayer *videoPlayer = [[AVPlayer alloc] initWithURL:fileUrl];
AVPlayerViewController *videoController = [AVPlayerViewController new];
videoController.player = videoPlayer;
[self presentViewController:videoController animated:YES completion:nil];}