0

这是我的第一篇文章,我有一个完全被难住的问题。我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 ];

     }
4

1 回答 1

0
  1. 调整您的项目以在后台播放音频(请参阅此处
  2. 通过开始播放

    dispatch_async(dispatch_get_main_queue(), ^{
        [avqueueplayerinstance play];            
    });//end block`
    
于 2014-02-14T06:46:35.893 回答