2017-09-03 76 views
2

我似乎無法在我的視頻中創建「完成」按鈕。我已附上我的代碼如下:AVPlayerViewController沒有「完成」按鈕目標C

- (IBAction)playVideoButton:(id)sender { 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Video1" ofType:@"mp4"]; 

    NSURL *url = [NSURL fileURLWithPath:path]; 

    AVPlayer *player = [AVPlayer playerWithURL:url]; 
    AVPlayerViewController *controller = [[AVPlayerViewController alloc]init]; 
    controller.player = player; 
    //to view on full UIScreen 

    self.mc = controller; 
    controller.view.frame = self.view.bounds; 
    [self.view addSubview:controller.view]; 
    [player play];  
} 
+0

你可以附上正在運行的應用程序的屏幕截圖嗎? –

+0

我似乎不能添加圖片,但它只是在左上角添加一個「完成」按鈕來關閉視頻並返回到初始屏幕。 – user8555346

+0

你可以嘗試MPMoviePlayerController而不是AVPlayerViewController.Below鏈接可能對你有用http://freefeast.info/tutorials-for-beginners/iphone-development/play-a-video-using-mpmovieplayercontroller/ –

回答

1

您沒有正確使用AVPlayerViewController類。這裏是你應該如何使用它:

- (IBAction)playVideoButton:(id)sender { 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Video1" ofType:@"mp4"]; 

    NSURL *url = [NSURL fileURLWithPath:path]; 

    AVPlayer *player = [AVPlayer playerWithURL:url]; 
    AVPlayerViewController *controller = [[AVPlayerViewController alloc]init]; 
    controller.player = player; 
    [self presentViewController:controller animated:YES completion:nil]; 
} 

就這樣,你就會有「完成」按鈕帶有AVPlayerViewController與其他播放按鈕(如暫停/恢復等)一起。

+0

啊,是的,我明白爲什麼,非常感謝! – user8555346

+0

@ user8555346這裏是來自Apple的示例代碼,如果您想了解更多關於自定義視頻播放佈局等信息,請訪問https://developer.apple.com/library/content/samplecode/AVPlayerDemo/Introduction/Intro.html –

+0

@ user8555346如果這個答案幫了你,接受它。 – the4kman