AVPlayer auto play

How to make avplayer in osx autoplay?

I tried this:

NSURL *videoURL = [[NSBundle mainBundle]
URLForResource:@"video name" withExtension:@"mp4"];
self.movieView.player = [AVPlayer playerWithURL:videoURL];

is it not auto-playing?

+4
source share
2 answers

You did not call the playback method.

[self.movieView.player play]
+7
source

I think AVplayer first checks for a URL or not. You must add an observer to it.

 [avplayerObject addObserver:self forKeyPath:@"status" options:0 context:nil];

#pragma Observer for Player status.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (object == _audioPlayer && [keyPath isEqualToString:@"status"]) {
        if (_audioPlayer.status == AVPlayerStatusFailed) {
            NSLog(@"AVPlayer Failed");
            loader.hidden = true;
        } else if (_audioPlayer.status == AVPlayerStatusReadyToPlay) {
            NSLog(@"AVPlayerStatusReadyToPlay");
                [_audioPlayer play];

        } else if (_audioPlayer.status == AVPlayerItemStatusUnknown) {
            NSLog(@"AVPlayer Unknown");

        }
        [_audioPlayer removeObserver:self forKeyPath:@"status" context:nil];
    }

}
+3
source

All Articles