在 iPhone 上使用 kAudioSessionProperty_OtherMixableAudioShouldDuck
我试图从 iPhone 上的 kAudioSessionProperty_OtherMixableAudioShouldDuck 属性中获得一致的行为,以允许 iPod 音乐混合,但我遇到了麻烦。在我的应用程序开始时,我设置了一个环境类别,如下所示:
-(void) initialize
{
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
}
稍后,当我尝试播放音频时,我使用以下方法设置鸭子属性:
//this will crossfade the audio with the ipod music
- (void) toggleCrossfadeOn:(UInt32)onOff
{
//crossfade the ipod music
AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck,sizeof(onOff),&onOff);
AudioSessionSetActive(onOff);
}
我在播放音频之前将其称为传递数字“1”,如下所示:
[self toggleCrossfadeOn:1];
[player play];
然后当我的应用程序的音频完成时,使用播放停止回调再次调用 crossfade 方法,传递一个零,如下所示:
-(void) playbackIsStoppingForPlayer:(MQAudioPlayer*)audioPlayer
{
NSLog(@"Releasing player");
[audioPlayer release];
[self toggleCrossfadeOn:0];
}
在我的生产应用程序中,这个确切的代码按预期工作,导致 iPod 在播放我的应用程序的音频之前淡出,然后在播放时淡入音频播放完毕。在我最近开始的一个新项目中,我得到了不同的行为。 iPod 音频逐渐减弱,并且永远不会淡入。在我的制作应用程序中,我使用 AVAudioPlayer,在我的新应用程序中,我编写了一个使用音频队列的自定义音频播放器。有人可以帮助我理解这些差异以及如何正确使用这个 API 吗?
I'm trying to get consistant behavior out of the kAudioSessionProperty_OtherMixableAudioShouldDuck property on the iPhone to allow iPod music blending and I'm having trouble. At the start of my app I set an Ambient category like so:
-(void) initialize
{
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
}
Later on when I attempt to play audio I set the duck property using the following method:
//this will crossfade the audio with the ipod music
- (void) toggleCrossfadeOn:(UInt32)onOff
{
//crossfade the ipod music
AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck,sizeof(onOff),&onOff);
AudioSessionSetActive(onOff);
}
I call this passing a numeric "1" just before playing audio like so:
[self toggleCrossfadeOn:1];
[player play];
I then call the crossfade method again passing a zero when my app's audio completes using a playback is stopping callback like so:
-(void) playbackIsStoppingForPlayer:(MQAudioPlayer*)audioPlayer
{
NSLog(@"Releasing player");
[audioPlayer release];
[self toggleCrossfadeOn:0];
}
In my production app this exact code works as expected, causing the ipod to fade out just before playing my app's audio then fade back in when the audio finishes playing. In a new project I recently started, I get different behavior. The iPod audio fades down and never fades back in. In my production app I use the AVAudioPlayer where in my new app I've written a custom audio player that uses audio queues. Could somebody help me understand the differences and how to properly use this API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚发现了问题的一部分。这似乎与时间有关。我相信当我调用将音频混合回来时,我的自定义播放器仍在为音频系统提供信号。如果我在playbackIsStoppingForPlayer方法中设置断点,那么它似乎会按预期工作,因为断点停止执行的时间足够长,足以满足剩余的时间音频完成音频队列的传输。
I just found part of the problem. It appears to be timing related. I believe my custom player is still feeding the audio system when I make the call to blend the audio back in. If I set a break point in the playbackIsStoppingForPlayer method then it appears to work as expected as the breakpoint stops execution long enough for the remaining audio to finish making its way through the audio queues.
在我们的一个应用中,我们调用
AudioSessionSetActive(YES)
,然后在想要删除闪避时立即调用AudioSessionSetActive(NO)
,以解决此问题。In one of our apps, we call
AudioSessionSetActive(YES)
and then immediately callAudioSessionSetActive(NO)
when we want to remove ducking, to work around this problem.