因此,我有一个应用程序可以播放一堆歌曲,而用户可以翻阅漫画书。我使用AVAudioPlayer,并且已将其设置为按设置的顺序播放歌曲。因此,当一首歌曲结束时,下一首将播放。当应用程序打开时,它可以完美地工作。当应用程序在后台运行时会出现问题。我将应用设置为在后台播放,效果很好。因此,当用户按下主屏幕时,音乐将继续播放。当歌曲结束时会出现问题,应该像在打开应用程序时一样播放下一首歌曲。相反,什么也没有发生。根据我的NSLog语句,正确的方法被调用,但是什么也没有发生。这是我的代码:
- (void)audioPlayerDidFinishPlaying: (AVAudioPlayer *)player successfully: (BOOL) flag {
NSLog(@"Song finished");
if ([songSelect isEqualToString: @"01icecapades"]) {
isPlay = @"yes";
songSelect = @"02sugarcube";
imageSelect = @"playbanner02";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) withObject:nil afterDelay:0];
}
else if ([songSelect isEqualToString: @"02sugarcube"]) {
isPlay = @"yes";
songSelect = @"03bullets";
imageSelect = @"playbanner03";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) withObject:nil afterDelay:0];
}
else if ([songSelect isEqualToString: @"03bullets"]) {
isPlay = @"yes";
songSelect = @"04satanama";
imageSelect = @"playbanner04";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) withObject:nil afterDelay:0];
}
else if ([songSelect isEqualToString: @"04satanama"]) {
isPlay = @"yes";
songSelect = @"05uglyjoke";
imageSelect = @"playbanner05";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) withObject:nil afterDelay:0];
}
else if ([songSelect isEqualToString: @"05uglyjoke"]) {
isPlay = @"yes";
songSelect = @"01icecapades";
imageSelect = @"playbanner01";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) withObject:nil afterDelay:0];
}}
上面的代码可以识别正在播放的歌曲,然后设置正确的歌曲。然后,它将触发另一种设置播放器的方法。
- (void)triggerSong {
NSLog(@"triggerSong called");
NSString *path;
NSError *error;
// Path the audio file
path = [[NSBundle mainBundle] pathForResource:songSelect ofType:@"mp3"];
// If we can access the file...
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
// Setup the player
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
//player = [initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
[player setDelegate: self];
// Set the volume (range is 0 to 1)
player.volume = 1.0f;
[player prepareToPlay];
[player setNumberOfLoops:0];
[player play];
NSLog(@"player play");
[error release];
player.delegate = self;
// schedules an action every second for countdown
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTimeLeft) userInfo:nil repeats:YES];
}}
现在,我假设这不是执行此操作的最佳方法,但是当应用程序处于前台状态时,它的效果很好。我一直在浏览文档,但似乎找不到导致此问题的原因。我希望有人可能看到我的方法有错误。就像我之前说过的那样,triggerSong方法中的两个NSLogs被调用,所以我看不到为什么未调用AVAudioPlayer(播放器)。
另外,我的info.plist中有正确的设置,而我的viewDidLoad中有此设置:
//Make sure the system follows our playback status
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
感谢您的任何见解。非常感激。
最佳答案
Relevant discussion
简短答案:
您需要在第一个视图控制器的init或viewDidLoad方法中使用以下代码:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
带示例的长答案:
这是我的例子。像您一样,我从一个可以在后台播放音乐但在第一个剪辑结束后就无法继续播放的应用开始。我制作了原始Music.mp3的副本,并将其命名为Music2.mp3。我的意图是在Music.mp3结束时立即播放Music2.mp3(audioPlayerDidFinishPlaying :)。我忙了一段时间的后台任务,直到没有后台任务就可以正常工作:
-(id)init{
self = [super initWithNibName:@"MediaPlayerViewController" bundle:nil];
if(self){
//Need this to play background playlist
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
//MUSIC CLIP
//Sets up the first song...
NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"Music" ofType:@"mp3"];
if(musicPath){
NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
[audioPlayer setDelegate:self];
}
}
return self;
}
-(IBAction)playAudioFile:(id)sender{
if([audioPlayer isPlaying]){
//Stop playing audio and change test of button
[audioPlayer stop];
[sender setTitle:@"Play Audio File" forState:UIControlStateNormal];
}
else{
//Start playing audio and change text of button so
//user can tap to stop playback
[audioPlayer play];
[sender setTitle:@"Stop Audio File" forState:UIControlStateNormal];
}
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[audioButton setTitle:@"Play Audio File" forState:UIControlStateNormal];
[playRecordingButton setTitle:@"Play Rec File" forState:UIControlStateNormal];
//PLAY THE SECOND SONG
NSString *musicPath2 = [[NSBundle mainBundle] pathForResource:@"Music2" ofType:@"mp3"];
if(musicPath2){
NSURL *musicURL2 = [NSURL fileURLWithPath:musicPath2];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL2 error:nil];
[audioPlayer setDelegate:self];
NSLog(@"Play it again: \n%@", musicPath2);
[audioPlayer play];
}
}
最终结果是,即使应用程序在后台,我的应用程序现在仍可以连续循环播放Music2.mp3。