在AVPlayer中显示播放控件

6

我可以运行视频,但无法看到控制按钮,如暂停和滑块等,以与视频进行交互。有类似的问题被问到(在这里看),但我不理解答案。


1
我建议使用苹果文档中提供的精彩教程和演示。它包含了你所需要的所有控件。 - Dipen Panchasara
1
谢谢Dipen,我已经看过这个例子了,它非常有用,但它没有使用AVPlayerViewController,而是使用系统播放控件。 - Utsav Dusad
这不是AVPlayerViewController,而是纯粹的AVPlayer示例。请再次检查。 - Dipen Panchasara
1
是的,那就是我的意思,这是一个纯AVPlayer示例。我想要一个带有系统播放控件(使用AVPlayerViewController完成)的示例,这在WWDC 2014中得到了很好的解释。 - Utsav Dusad
那么你应该使用 MPMoviePlayerController,这是唯一能给你想要的结果的选项。 - Dipen Panchasara
MPMoviePlayerController从iOS9.0开始已经被弃用。我认为AVPlayerViewController可以完成与MPMoviePlayerController相同的工作。 - Utsav Dusad
1个回答

5

我已经找到了带有说明的答案。请点击这里。要显示系统的播放控件,如播放、暂停按钮等,我们需要使用AVPlayerViewController。以下是Objective-C的实现。

@interface ViewController ()
@property (strong,nonatomic) AVPlayerViewController *avPlayerViewController;
@property (strong,nonatomic) AVPlayer *avPlayer;
@end

    - (void)viewDidLoad {
    [super viewDidLoad];
    //URL for the file. It can be over HTTP or local file URL. 
    NSURL *folderURL=[NSURL fileURLWithPath:uploadFolderLocation];
    NSURL *movieURL=[folderURL URLByAppendingPathComponent:@"video.mp4"];

    self.avPlayer = [AVPlayer playerWithURL:movieURL];

    self.avPlayerViewController=[[AVPlayerViewController alloc]init];
    self.avPlayerViewController.player=self.avPlayer;    
}

- (IBAction)playButtonTapped:(id)sender {
    //Trigger the video to play

    //AVPlayer object can direct its visual output to AVPlayer. AVPlayerVC is a AVPlayerViewController. You can add it via objects in bottom-right corner.
    AVPlayerVC *avPLayerVC=[[AVPlayerVC alloc] init];
    avPLayerVC.player=self.avPlayer;

    [self addChildViewController:self.avPlayerViewController];
    [self.view addSubview:self.avPlayerViewController.view];
    self.avPlayerViewController.view.frame=self.view.frame;
    [self.avPlayerViewController.player play];
   }

带有控制按钮的视频图像: enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接