简体   繁体   English

使用MPMusicPlayer的iPhone App背景

[英]iPhone App backgrounding with MPMusicPlayer

I'm working on an iPhone iOS4 application that incorporates playing music from the user's iPod library. 我正在开发一个iPhone iOS4应用程序,它结合了用户iPod库中播放的音乐。 I also want to keep track of what songs have been played and be able to change the song randomly, even while in the background. 我还想跟踪已播放的歌曲,并能够随机更改歌曲,即使在后台也是如此。 So I set the music player using: 所以我设置音乐播放器使用:

[self setMusicPlayer: [MPMusicPlayerController iPodMusicPlayer]];

Now, I want this application to continue to run and play music in the background, so I have set: 现在,我希望这个应用程序继续在后台运行和播放音乐,所以我设置:

Required background modes: App plays audio

The problem I'm having is that my application loses control when it is moved into the background (when applicationDidEnterBackground is called, ie. on app switches). 我遇到的问题是我的应用程序在移动到后台时失去控制权(当调用applicationDidEnterBackground时,即在app开关上)。 Since I'm using the iPodMusicPlayer the music continues to play but my app does not have control and therefore can't track or change the song. 由于我正在使用iPodMusicPlayer音乐继续播放,但我的应用程序无法控制,因此无法跟踪或更改歌曲。

Now, the Apple documentation states that your application should continue to execute in the background using this required background modes tag, but mine does not. 现在,Apple文档声明您的应用程序应该继续使用此必需的后台模式标记在后台执行,但我的应用程序不会。 Is it because I'm using MPMusicPlayer? 是因为我正在使用MPMusicPlayer吗? Is there any way to get around it? 有没有办法绕过它? Any ideas? 有任何想法吗?

PS. PS。 I'm also trying to get the remote locked and multitasking iPod controllers to work with my application. 我也试图让远程锁定和多任务iPod控制器与我的应用程序一起工作。 I'm using the code below, but remoteControlReceivedWithEvent never gets called! 我正在使用下面的代码,但是从不调用remoteControlReceivedWithEvent! Does it work with MPMusicPlayer? 它适用于MPMusicPlayer吗? I've only seen it with AVAudioPlayer. 我只用AVAudioPlayer看过它。

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    NSLog(@"remoteControlReceivedWithEvent");
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlTogglePlayPause:
            NSLog(@"Play Pause");
            break;
        case UIEventSubtypeRemoteControlNextTrack:
            NSLog(@"Next");
            break;
        default:
            break;
    }
}

- (BOOL)canBecomeFirstResponder {
    NSLog(@"canBecomeFirstResponder");
    return YES;
 }

- (void) viewWillAppear:(BOOL)animated{
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

You are correct. 你是对的。 Your iPhone app will not run in the background while using MPMusicPlayerController . 使用MPMusicPlayerController您的iPhone应用程序将无法在后台运行。 This will also prevent you from receiving remote control events. 这也会阻止您接收远程控制事件。

If you want to play audio from the iPod library and have your app continue running in the background, you must use the lower-level AVPlayer class. 如果要播放iPod库中的音频并让应用程序继续在后台运行,则必须使用较低级别的AVPlayer类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM