Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 598
Utsav Dusad Avatar asked Apr 05 '16 20:04

Utsav Dusad


1 Answers

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

like image 179
Utsav Dusad Avatar answered Oct 23 '22 05:10

Utsav Dusad