我环顾四周,但找不到AVPlayer class
的委托协议。是什么给了什么?
我正在使用它的子类AVQueuePlayer
来播放AVPlayerItems
数组,每个数组都从一个URL加载。有什么方法可以在歌曲播放完毕后调用方法吗?特别是在队列的最后?
如果这不可能,有什么办法可以在歌曲STARTS播放后调用方法,缓冲后?我正试图在那里获得一个加载图标,但它会在音乐实际开始之前关闭图标,即使它是在[audioPlayer play]
动作之后。
答案 0 :(得分:147)
是的,AVPlayer类没有像AVAudioPlayer这样的委托协议。您需要订阅AVPlayerItem上的通知。您可以使用在AVPlayer上传递给-initWithURL:
的相同URL创建AVPlayerItem。
-(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
}
答案 1 :(得分:8)
是。将KVO观察者添加到玩家的状态或比率:
- (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...];
}
所以基本上就是这样。
免责声明:我在这里写道,所以我没有检查代码是否良好。我之前从未使用过AVPlayer,但它应该是正确的。
答案 2 :(得分:5)
Swift 3 - 每次向播放器添加视频时,我都会向AVPlayerItem
添加观察者:
func playVideo(url: URL) {
let playerItem = AVPlayerItem(asset: AVURLAsset(url: someVideoUrl))
NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidPlayToEndTime), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)
self.player.replaceCurrentItem(with: playerItem)
self.player.play()
}
func playerItemDidPlayToEndTime() {
// load next video or something
}
答案 3 :(得分:1)
Apple docs AVFoundation Programming Guide中有很多信息(查找监控播放部分)。它似乎主要是通过KVO,所以如果你不太熟悉,你可能希望对此有所了解(Key Value Observing ProgrammingGuide有指南。
答案 4 :(得分:1)
我正在使用这个,它有效:
_player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:_playingAudio.url]];
CMTime endTime = CMTimeMakeWithSeconds(_playingAudio.duration, 1);
timeObserver = [_player addBoundaryTimeObserverForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:endTime]] queue:NULL usingBlock:^(void) {
[_player removeTimeObserver:timeObserver];
timeObserver = nil;
//TODO play next sound
}];
[self play];
其中_playingAudio
是我的自定义类,其中包含一些属性,timeObserver
为id
ivar。