[英]iOS AVPlayer v. MPMusicPlayerController
I am writing a music playing app which performs a task between each song. 我正在编写一个音乐播放应用程序,该程序在每首歌曲之间执行一项任务。 I need to do this even when the app is in the background, so I need to know when a song has completed.
即使应用程序在后台,我也需要这样做,所以我需要知道歌曲何时完成。 At the moment I am using
AVPlayer
which sends notifications even when the app is in the background, but is unable to play songs from the user's iCloud. 目前,我正在使用
AVPlayer
,即使应用程序在后台,它也可以发送通知,但无法播放用户iCloud上的歌曲。 MPMediaPlayerController
can play iCloud songs but doesn't send notifications when the app is in the background (which is essential to my app). MPMediaPlayerController
可以播放iCloud歌曲,但在后台运行时不发送通知(这对于我的应用程序至关重要)。
So, does anyone know either 所以,有人知道吗
AVPlayer
play iCloud songs, or AVPlayer
播放iCloud歌曲的任何巧妙方法,或者 MPMusicPlayerController
has completed when the app is in the background? MPMusicPlayerController
播放的歌曲时,让我的应用识别吗? Have you tried using NSNotification Center and one of these two observers? 您是否尝试过使用NSNotification Center和这两个观察者之一?
MPMusicPlayerControllerPlaybackStateDidChangeNotification
or MPMusicPlayerControllerNowPlayingItemDidChangeNotification
. MPMusicPlayerControllerPlaybackStateDidChangeNotification
或MPMusicPlayerControllerNowPlayingItemDidChangeNotification
。
Also, you need to use beginGeneratingPlaybackNotifications()
on your instance of MPMusicPlayerController.applicationMusicPlayer()
or MPMusicPlayerController.systemMusicPlayer()
此外,您还需要使用
beginGeneratingPlaybackNotifications()
在您的实例MPMusicPlayerController.applicationMusicPlayer()
或MPMusicPlayerController.systemMusicPlayer()
To build on @Kim's answer. 以@Kim的答案为基础。 You can use the
NSNotificationCenter
and add ObserverEvents
. 您可以使用
NSNotificationCenter
并添加ObserverEvents
。 I use the MPMusicPlayerController
class for my application, and I've registered the application to use the NSNotificationCenter
properties so I can call certain methods during differing events. 我为我的应用程序使用
MPMusicPlayerController
类,并且已经注册了该应用程序以使用NSNotificationCenter
属性,因此我可以在不同的事件期间调用某些方法。
For instance, while using the SystemMusicPlayer
property if you exit and terminate the application process, the music keeps playing. 例如,在使用
SystemMusicPlayer
属性时,如果退出并终止应用程序进程,则音乐将继续播放。 If the user wanted to stop the music when they exityou could call this: 如果用户要在退出时停止播放音乐,可以致电:
-(void) registerMediaPlayerNotifications {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(stopMusicWhenApplicationQuits)
name:UIApplicationWillTerminateNotification
object:[UIApplication sharedApplication]];
[musicPlayer beginGeneratingPlaybackNotifications];
}
Where you see the @selector
that is the method that will be fired when the application receives the UIApplicationWillTerminateNotification
event. 您在其中看到
@selector
的方法,当应用程序收到UIApplicationWillTerminateNotification
事件时将触发该方法。 Which in the musicPlayer
you can say 您可以在
musicPlayer
中说什么
[self.musicPlayer stop];
So for the question you have, you can still use the MediaPlayer
framework, use the MPMusicPlayerController
class and call various methods during different application runtime stages using the NSNotificationCenter
properties. 因此,对于您遇到的问题,您仍然可以使用
MediaPlayer
框架,使用MPMusicPlayerController
类,并使用NSNotificationCenter
属性在不同的应用程序运行时阶段调用各种方法。
Hope this helps, let me know if you have any further questions. 希望对您有所帮助,如有其他问题,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.