没有AVPlayer代表?歌曲播放完毕后如何追踪? Objective C iPhone开发 [英] No AVPlayer Delegate? How to track when song finished playing? Objective C iPhone development
问题描述
我环顾四周但是找不到 AVPlayer类
的委托协议。给出了什么?
I've looked around but I can't find a delegate protocol for the AVPlayer class
. What gives?
我正在使用它的子类 AVQueuePlayer
来播放 AVPlayerItems
,每个都从URL加载。有什么方法可以在歌曲播放完毕后调用方法吗?特别是在队列的最后?
I'm using its subclass, AVQueuePlayer
, to play an array of AVPlayerItems
, each loaded from a URL. Is there any way I can call a method when a song finishes playing? Notably at the end of the queue?
如果那不可能,有什么办法可以在歌曲STARTS播放后调用方法,缓冲后?我正试图在那里获得一个加载图标,但它在音乐实际开始之前关闭了图标,即使它是在 [audioPlayer play]
动作之后。
And if that's not possible, is there any way I could call a method when the song STARTS playing, after buffering? I'm trying to get a loading icon in there but it turns the icon off before the music actually begins, even though it's after the [audioPlayer play]
action.
推荐答案
是的,AVPlayer类没有像AVAudioPlayer这样的委托协议。您需要订阅AVPlayerItem上的通知。您可以使用在AVPlayer上传递给 -initWithURL:
的相同URL创建AVPlayerItem。
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
}
这篇关于没有AVPlayer代表?歌曲播放完毕后如何追踪? Objective C iPhone开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!