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

前端 未结 5 1235
死守一世寂寞
死守一世寂寞 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:38

    Yes. Add a KVO observer to the player's status or rate:

    - (IBAction)go {
       self.player = .....
       self.player.actionAtItemEnd = AVPlayerActionStop;
       [self.player addObserver:self forKeyPath:@"rate" options:0 context:0]; 
    }
    
    - (void)stopped {
        ...
        [self.player removeObserver:self]; //assumes we are the only observer
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
        if (context == 0) {
            if(player.rate==0.0) //stopped
                [self stopped];
        }
        else
            [super observeVal...];
    }
    

    So basically, that's it.

    Disclaimer: I wrote that in here so I didn't check if the code was good. ALSO I never used AVPlayer before but it should be about right.

提交回复
热议问题