kAudioSessionProperty_OtherMixableAudioShouldDuck 有时音乐不会 unduck
我正在使用 MPMusicPlayerController 通过 iPodMusicPlayer 播放设备中的音乐。并定期使用 AVPlayer 播放警报。代码是这样的
-(void) duckMusic
{
UInt32 on=1;
AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck,sizeof(on),&on);
AudioSessionSetActive(true);
}
-(void) unDuckMusic
{
UInt32 on=0;
AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck,sizeof(on),&on);
AudioSessionSetActive(false);
}
我在播放警报之前调用第一个方法,在收到通知后调用第二个方法 AVPlayerItemDidPlayToEndTimeNotification
我遇到的问题是,有时音乐在警报完成后不会淡出。通过一些调试,我发现音频会话属性是 已正确设置,但由于某种原因被忽略。
我在测试时还注意到,当音乐没有淡出时,如果我只是在设备上手动暂停并播放音乐,它就会变得正常。
如何以编程方式处理这个问题?
编辑:
当应用程序在后台运行时就会出现问题
I am using MPMusicPlayerController to play music from device with iPodMusicPlayer. And at regular intervals play alerts with AVPlayer. Code goes like this
-(void) duckMusic
{
UInt32 on=1;
AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck,sizeof(on),&on);
AudioSessionSetActive(true);
}
-(void) unDuckMusic
{
UInt32 on=0;
AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck,sizeof(on),&on);
AudioSessionSetActive(false);
}
I call the first method just before playing the alert and I call the second method after recieving notification AVPlayerItemDidPlayToEndTimeNotification
The problem I am getting is that some times the music does not fade back after alert is completed. With some debugging I have found that the audio session property is
being set properly but it is being ignored for some reason.
I have also noticed while testing, that when music does not fade back, if I just pause and play music manually on device it becomes to normal.
How to handle this programmatically?
Edit:
The problem is coming when application is running in background
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要在 unDuckMusic 中设置该属性,停用会话应该足以让混合音频恢复到正常音量。
当我的音频会话在后台时无法正确处理音频中断时,我遇到了类似的问题,这导致音频会话调用以不可预测的方式失败。所以一定要处理好这些。
You shouldn't need to set the property in unDuckMusic, deactivating the session should be enough to allow mixed audio to return to normal volume.
I had a similar problem when my audio session was not correctly handling an audio interruption while backgrounded, which caused audio session calls to fail in unpredictable ways. So make sure you handle those.