Always Show Avplayer Controls

Display playback controls in AVPlayer

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: Sample Image

AVPlayerViewController inside a view is not showing the playback controls

This is an expected behaviour:

AVPlayerViewController is designed such that when in full screen, the
full playback experience (scrubbing, info panel access, etc) are all
available. When in a space less than full screen, the assumption is
that it is just one of several interactive elements on screen, and
thus the view should not absorb all touch surface events as transport
control.

You can read more about this on this thread: https://forums.developer.apple.com/thread/19526

AVPlayer view in UIView is missing controls

Try setting showsPlaybackControls and requiresLinearPlayback to true,

playerViewController.showsPlaybackControls = YES;
playerViewController.requiresLinearPlayback = YES;

Currently, you didn't add playerViewController as child viewController so you will not be able to see anything. Add as below,

[self addChildViewController:playerViewController];
[self.view.layer addSublayer:playerLayer];
[self.view addSubview:playerViewController.view];


Related Topics



Leave a reply



Submit