ios - 在 AVPlayer 中显示播放控件

标签 ios objective-c avfoundation avplayer avplayerviewcontroller

我可以运行视频,但看不到暂停等控制按钮和与视频交互的 slider 。询问了类似的查询(see here),但我没有遵循答案。

最佳答案

我得到了描述的答案。 See here .要显示播放、暂停按钮等系统播放控件,我们需要 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

关于ios - 在 AVPlayer 中显示播放控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36436311/

相关文章:

ios - 使用 GPU 清除 CVPixelBufferRef

objective-c - AVPlayerLooper 不会循环播放多个视频

ios - Sprite Kit 中的简单碰撞

ios - 设置 UITextField 不可编辑或可编辑

ios - 我想使用CAPropertyAnimations在iOS中绘制平滑动画

ios - Xamarin iOS - 推送通知 - 区分已点击的推送通知已到达

objective-c - 如何为UIImage的drawAsPatternInRect指定图像偏移

ios - 使用 AVFoundation 框架进行条码扫描的错误结果

ios - 在 ios 中检测 Facetime 来电

ios - 如何使用 Swift 4 将字典对象插入 Firebase 数据库