这是我的第一篇文章,我有一个问题,我完全被难过。我有一个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 ];
}
答案 0 :(得分:0)
开始播放
dispatch_async(dispatch_get_main_queue(), ^{
[avqueueplayerinstance play];
});//end block`