2017-09-20 1 views
0

처음 비디오를 재생할 때 비디오가 완벽하게 잘 재생되지만 전화 갤러리에서 비디오를 선택하여 바꾸면 비디오가 2 초 후에 검게 변하고 끝까지 오디오가 재생됩니다. 여기에 비디오 플레이어를 추가 : 하나의 갤러리에서 선택과 AVPlayerItem을 교체 한 후 몇 초 후에 비디오가 검게됩니다.

-(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]; 
}]; 

답변

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];} 
+0

먼저 내 질문에 답변 해주십시오. 이것은 내가 요구하는 것이 아닙니다. –

관련 문제