No AVPlayer Delegate? How to track when song finished playing? Objective C iPhone development

前端 未结 5 1226
死守一世寂寞
死守一世寂寞 2020-11-27 14:09

I\'ve looked around but I can\'t find a delegate protocol for the AVPlayer class. What gives?

I\'m using its subclass, AVQueuePlayer, to pl

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 14:51

    Yes, the AVPlayer class does not have a delegate protocol like the AVAudioPlayer. You need to subscribe to notifications on an AVPlayerItem. You can create an AVPlayerItem using the same URL that you would otherwise pass to -initWithURL: on AVPlayer.

    -(void)startPlaybackForItemWithURL:(NSURL*)url {
    
        // First create an AVPlayerItem
        AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url];
    
        // Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
    
        // Pass the AVPlayerItem to a new player
        AVPlayer* player = [[[AVPlayer alloc] initWithPlayerItem:playerItem] autorelease];
    
        // Begin playback
        [player play]
    
    } 
    
    -(void)itemDidFinishPlaying:(NSNotification *) notification {
        // Will be called when AVPlayer finishes playing playerItem
    }
    

提交回复
热议问题