我为我的应用程序做了一个报警功能,当我设置警报火灾时,我可以用AVAudioPlayer播放一首音乐曲目,它在后台模式下工作得很好。但如果此时我打开Youtube应用程序,播放一段视频,那么AVAudioPlayer就无法工作。
请告诉我为什么会发生这种情况,如何解决这一问题,如何同时发挥两者的作用。
这是我实现的代码:
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"remind2"
withExtension:@"mp3"];
avSound = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundURL error:nil];
[avSound setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[avSound prepareToPlay];
[avSound setNumberOfLoops:10];
[avSound setVolume:1.0];
[avSound play];谢谢!
发布于 2016-08-02 09:31:23
替换这一行
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];通过这一点:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];我解决了我的问题
对这个问题我花了两天的时间,只有当我问Google为什么Youtube停止我的音频流并且这个结果搜索帮助我时,我才能找到解决方案。
发布于 2016-08-01 03:59:57
这是因为Youtube应用程序还在plist中设置了属性"UIBackgroundMode"属性。即使你查看其他的音乐应用,如gaana,savan等,这也会发生在所有的音乐应用中。
https://stackoverflow.com/questions/38690705
复制相似问题