如果 iPodMusicPlayer 在后台创建,则不会发送通知
如果 iPodMusicPlayer 是在后台创建的,那么我不会发送有关播放状态更改的通知。
这是代码:
- (void)initMusicPlayer {
musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(musicPlayerStateChanged:)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:musicPlayer];
[notificationCenter addObserver:self selector:@selector(musicPlayerStateChanged:)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
object:musicPlayer];
[musicPlayer beginGeneratingPlaybackNotifications];
}
- (void)viewDidLoad {
[self performSelectorInBackground:@selector(initMusicPlayer) withObject:nil];
}
有什么方法可以在后台创建 iPodMusicPlayer 吗?否则,如果它是在主线程上创建的,它会阻塞执行一段时间。
If iPodMusicPlayer is created in background, then I doesn't send notifications about playback state changes.
Here is code:
- (void)initMusicPlayer {
musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(musicPlayerStateChanged:)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:musicPlayer];
[notificationCenter addObserver:self selector:@selector(musicPlayerStateChanged:)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
object:musicPlayer];
[musicPlayer beginGeneratingPlaybackNotifications];
}
- (void)viewDidLoad {
[self performSelectorInBackground:@selector(initMusicPlayer) withObject:nil];
}
Is there some way how to create iPodMusicPlayer in background? Otherwise if it is created on the main thread, it blocks executions for a while.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
音乐播放器必须在主线程上使用。
https://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMusicPlayerController_ClassReference/Reference/Reference.html%23//apple_ref/doc/uid/TP40008221
A music player must be used on main thread.
https://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMusicPlayerController_ClassReference/Reference/Reference.html%23//apple_ref/doc/uid/TP40008221