- (IBAction)playOrPauseSound:(id)sender;
{
[_audioPlayer play];
[[NSNotificationCenter defaultCenter] addObserver:_audioPlayer selector:@selector(nextsong:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}
- (IBAction)nextsong:(id)sender {
//code
}
user2043050
问问题
852 次
1 回答
1
您应该将 notificationObserver 设置为,self
因为这是充当观察者的对象。您还应该将 notificationSender 设置为,_audioPlayer
因为这是发送AVPlayerItemDidPlayToEndTimeNotification
通知的对象。
此外,选择器方法应该有一个 NSNotification 实例作为唯一参数。所以我很想创建一个单独的方法来处理接收通知,然后调用 next song 方法,也许:
- (void)receivedNextSongNotification:(NSNotification *)notification
{
[self nextsong:nil];
}
所以总的来说,是这样的:
- (IBAction)playOrPauseSound:(id)sender
{
[_audioPlayer play];
[[NSNotificationCenter defaultCenter] addObserver: self //will look for the selector in the current class
selector: @selector(playerItemDidPlayToEndTime:)
name: AVPlayerItemDidPlayToEndTimeNotification
object: _audioPlayer]; // the object that sends the notifications
}
- (void)playerItemDidPlayToEndTime:(NSNotification *)notification
{
[self nextsong:nil];
}
- (IBAction)nextsong:(id)sender
{
//code
}
还要确保在调用removeObserver:name:object:
之前self
或被_audioPlayer
释放。
希望有帮助。
于 2013-07-10T12:28:51.500 回答