0

我的项目中有一个功能支持即使在后台状态下也可以播放新的音频文件。我使用 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
     {

     }
 }
}
4

1 回答 1

0

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

以下是我的代码:</p>

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

于 2013-03-18T08:23:04.890 回答