根据我们的要求,即使应用程序处于后台,我们也需要播放声音。我们正在使用[AVAudioPlayer Play]
来播放声音。
如果应用程序在前台,它将起作用。但是,如果应用程序在后台运行,并且我们也在音乐播放器中播放歌曲,然后控制权转到[AVAudioPlayer Play]
语句,则什么也不会发生。
我们已经遍历了许多链接,在plist中添加了背景模式,还添加了[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
,但是事情没有解决。
请提出是否还有其他选择。
最佳答案
只需在初始化AVAudioPlayer
实例的位置添加以下三行:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"mySong" ofType:@"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
// These lines need to be added:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer play];
关于iphone - AVAudioPlayer在后台播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14874594/