IOS AVPlayerViewController does not display playback controls

Any idea why I can get the AVPlayerViewController class to play content, but can't get it to display playback controls? The player downloads the content as expected, but what is it.

In my MediaPlayerViewController.m class, I have:

#pragma mark - Player - (void)setupPlayer{ // Sample URLs to use either a local file or streaming URL NSURL *url = [[NSBundle mainBundle] URLForResource:@"Besan" withExtension:@"mp4"]; //url = [NSURL URLWithString:@"http://www.youtube.com/watch?v=qcI2_v7Vj2U"]; // Create an instance of the AVPlayer object self.player = [AVPlayer playerWithURL:url]; // Keep an eye on the status of our player [self.player addObserver:self forKeyPath:@"status" options:0 context:nil]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if (object == self.player && [keyPath isEqualToString:@"status"]) { if (self.player.status == AVPlayerStatusFailed){ NSLog(@"AVPlayer setup failed"); } else if (self.player.status == AVPlayerStatusReadyToPlay) { NSLog(@"AVPlayer is ready to play"); [self.player play]; } } } 
+5
source share
3 answers

This is not a structural error, but related to how key monitoring works. Introducing

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 

in your ViewController, you override the implementation of AVPlayerViewController. The solution is to add context when registering the observer:

 static NSString* const AVPlayerStatusObservationContext = @"AVPlayerStatusObservationContext"; 

then register your observer as follows

 [self.player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionPrior context:(__bridge void *)(AVPlayerStatusObservationContext)]; 

and follow these steps in watchValueForKeyPath

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (context == (__bridge void *)(AVPlayerStatusObservationContext)) { NSLog(@"Change Object %@, Value %@",object,change); } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } 
+11
source

As I noted in my comment on the question, the controls disappear when you implement - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context in AVPlayerViewController. This may be an empty implementation; Player controls are not displayed. It should be a framework, and I am shocked (shocked!) That Apple did not notice this.

My solution is to implement an observer in another class. I created the VideoObserver class inherited from NSObject and set it up like this:

 @implementation VideoObserver - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"status"]) { NSLog(@"Change: %@", change); } } @end 

In the AVPlayerViewController, I set the observer as a property, and then use it to start the observation:

 self.observer = [VideoObserver new]; NSURL * videoURL = [NSURL URLWithString:@"http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"]; self.videoPlayer = [AVPlayer playerWithURL:videoURL]; [self.videoPlayer addObserver:self.observer forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial context:nil]; 

I posted my demo project on Github.

+4
source

It may be funny, but make sure the controls do not hide under the TabBar. They haven’t found a solution yet, but this little thing gave me some grief and took me a little time to understand, but the controls are actually still there.

0
source

Source: https://habr.com/ru/post/1206282/


All Articles