我的项目中有一个功能支持即使在后台状态下也可以播放新的音频文件。我使用 MPMoviePlayerController 类来播放音频文件。但是我发现它是在激活状态下启动的,那么它会在后台状态下继续播放相同的音频(如果音频文件没有播放完);但在后台状态下它不会启动新的音频。
如何支持在后台播放新音频?我发现iphone的音乐播放器可以支持在后台播放新音频,当设置runloop或随机播放模式时。
以下是我的代码来证明实际上的外观:
(void)applicationDidEnterBackground:(UIApplication *)application
{
if (pCotnroller1) {
[pCotnroller1 release];
}
pCotnroller1 = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://r.yhiker.com:9001/0086/32/05/060009/Data/6/00863205060009006_24Kbps.mp3"]];
pCotnroller1.shouldAutoplay = YES;
[pCotnroller1 play];
//it wasn't played here
}
(void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
if (pCotnroller1) {
switch (pCotnroller1.playbackState) {
case MPMoviePlaybackStateStopped:
NSLog(@"播放地址");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"播放暂停");
break;
case MPMoviePlaybackStateInterrupted:
NSLog(@"播放中断");
break;
case MPMoviePlaybackStateSeekingBackward:
break;
case MPMoviePlaybackStateSeekingForward:
break;
default:
break;
}
if (pCotnroller1.playbackState == MPMoviePlaybackStatePlaying) {
//it will come here,and play autoed here
// [pCotnroller1 play];
}
else
{
}
}
}