iOS7 AVQueueplayer后台循环(后台循环排队音乐)

时间:2014-01-13 04:26:59

标签: ios xcode avqueueplayer

这是我的第一篇文章,我有一个问题,我完全被难过。我有一个AVQueueplayer在服务器上发布了多个mp3。

当应用程序在前台时,应用程序会播放和循环。但是,当我按下主页按钮时,queueplayer会停止循环播放。

这是我到目前为止的代码

-(void)playselectedsong{

    NSError *sessionError = nil;
    [[AVAudioSession sharedInstance] setDelegate:self];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord     error:&sessionError];


    UInt32 doChangeDefaultRoute = 1;
    AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);





     NSString* musicURL= [NSString stringWithFormat:@"%@%@", getMusicURL, [musicArray     objectAtIndex:0]];
     NSString* musicURL2= [NSString stringWithFormat:@"%@%@", getMusicURL, [musicArray objectAtIndex:1]];



     NSArray *queue = @[[AVPlayerItem playerItemWithURL:[NSURL URLWithString:musicURL]], [AVPlayerItem playerItemWithURL:[NSURL URLWithString:musicURL2]]];
     AVQueuePlayer *qplayer = [[AVQueuePlayer alloc] initWithItems:queue];
     self.queuePlayer=qplayer;
     [qplayer play];
     qplayer.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;

     [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[queue lastObject]];


    }


    - (void)playerItemDidReachEnd:(NSNotification *)notification {

     //  code here to play next sound file
         NSLog(@"playeritemdidreachend");
         [self playselectedsong ];

     }

1 个答案:

答案 0 :(得分:0)

  1. 调整项目以在后台播放音频(请参阅here
  2. 开始播放

    dispatch_async(dispatch_get_main_queue(), ^{
        [avqueueplayerinstance play];            
    });//end block`