如何支持在后台播放音频文件

时间:2013-03-18 06:36:02

标签: audio

我的项目中有一个功能,即使在后台状态下也支持播放新的音频文件。我使用MPMoviePlayerController类来播放音频文件。   但是我发现它是在活动状态下启动的,然后它会在后台状态下继续播放相同的音频(如果音频文件未完成);但它不会在后台状态下启动新的音频。

如何支持在背景状态下播放新音频?     我发现iphone的音乐播放器可以支持在后台状态下播放新音频,当设置runloop或随机PlayMode时。

以下是我的代码,以证明其外观:

(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
     {

     }
 }
}

1 个答案:

答案 0 :(得分:0)

我尝试了一些在网上找到的方法,但它没有用。有人用这种方式来解决他的问题。我不知道为什么。

以下是我的代码:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [application beginReceivingRemoteControlEvents];
    [self updateUserLocation]; 
} 

- (void)updateUserLocation
{
    if (!pCotnroller1) {
    //        pCotnroller1 = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://r.yhiker.com:9001/0086/32/05/PP0041/Data/13/00863205PP0041013_24Kbps.mp3"]];

        NSString *audioDiskPath = [HelpTools returnSpotAudioDiskPath:@"00863205PP0041013"];
          pCotnroller1 = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:audioDiskPath/*@"/var/mobile/Applications/E7A06CB2-0856-407D-ABA1-2A846B65CD9D/Library/Caches/Data/0086/32/05/PP0041/Data/13/00863205060005016_24Kbps.mp3"*/]];

        pCotnroller1.shouldAutoplay = YES;
        if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
            [pCotnroller1 play];
            NSLog(@"进入后台");
            UIApplication *app = [UIApplication sharedApplication];
            UIBackgroundTaskIdentifier taskID = [app beginBackgroundTaskWithExpirationHandler:nil];
            if (bgTask != UIBackgroundTaskInvalid) {
                [app endBackgroundTask:bgTask];
                NSLog(@"允许后台播放");
            }

            bgTask = taskID;
        }
        else
        {
            NSLog(@"飞后台进行播放");
            [pCotnroller1 play];
        }
    }
}

控制台打印: 2013-03-18 16:20:11.844 Sz [1665:907] [MPAVController]自动播放:禁用自动播放暂停 2013-03-18 16:20:11.845 Sz [1665:907] [MPAVController]自动播放:禁用自动播放 2013-03-18 16:20:13.489 Sz [1665:907]文件存在 2013-03-18 16:20:18.017 Sz [1665:907] [MPAVController]自动播放:跳过自动播放,禁用(当前项目:1,播放器:0) 2013-03-18 16:20:18.998 Sz [1665:907]进入后台 2013-03-18 16:20:21.269 Sz [1665:907]播放地址 2013-03-18 16:20:26.251 Sz [1665:907] [MPAVController]自动播放:启用自动播放 2013-03-18 16:20:26.307 Sz [1665:907] [MPAVController]自动播放:可能跟上或完全缓冲:0 2013-03-18 16:20:26.309 Sz [1665:907] [MPAVController]自动播放:跳过自动播放,没有足够的缓冲来跟上。 2013-03-18 16:20:26.319 Sz [1665:907] [MPAVController]自动播放:启用自动播放 2013-03-18 16:20:26.338 Sz [1665:907] [MPCloudAssetDownloadController]请求媒体项ID的优先级:0