Search code examples
iosobjective-cavfoundationavplayeravplayerviewcontroller

Display playback controls in AVPlayer


I am able to run the video, but I am not able to see the control buttons like pause and the slider to interact with the video. Similar query is asked (see here) but I do not follow the answer.


Solution

  • I got the answer with description. See here. To display system playback controls like play, pause button etc. we need AVPlayerViewController. Below is the implementation in 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];
       }
    

    The image of the video with control buttons: enter image description here