播放结束后将音量控制返回到 iOS 设备铃声

发布于 2024-12-16 19:43:42 字数 845 浏览 1 评论 0原文

我有一个音频应用程序,它使用媒体播放音频会话类别来允许背景音频。音频会话初始化后,iOS 设备上的硬件音量按钮将控制音频会话的音量。

一旦音频播放停止,我想将对电话铃声的控制返回给硬件音量按钮,但我尝试通过停用音频会话来实现此目的并没有成功。

以下是我初始化和激活音频会话的方式:

AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, self);

AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, 
                                audioRouteChangeListenerCallback, 
                                self);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;

AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                        sizeof(sessionCategory),           
                        &sessionCategory);

以下是我尝试停用音频会话并将 iOS 设备铃声的控制权返回给硬件音量控件的方式:

AudioSessionSetActive(false);

据我所知,至少有一个应用程序其行为方式如下(Audible.com 的 iOS 应用程序)。有谁知道我可能做错了什么?

I have an audio app that uses the Media Playback audio session category to allow background audio. Once my audio session is initialized, the hardware volume buttons on the iOS device control the volume of my audio session.

Once audio playback stops, I'd like to return control of the phone ringer back to the hardware volume buttons but my attempt to do this by deactivating the audio session doesn't do the trick.

Here is how I'm initializing and activating my audio session:

AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, self);

AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, 
                                audioRouteChangeListenerCallback, 
                                self);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;

AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                        sizeof(sessionCategory),           
                        &sessionCategory);

Here is how I'm attempting to deactivate the audio session and return control of the iOS device's ringer back to the hardware volume controls:

AudioSessionSetActive(false);

There is at least one app that I know of that behaves this way (Audible.com's iOS app). Does anyone have any idea what I may be doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-23 19:43:42

在苹果文档中,我认为您必须实际删除侦听器。

抬头:
AudioSessionRemovePropertyListenerWithUserData

http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html

In apples documentation I think you are going to have to actually remove the listener.

Look up:
AudioSessionRemovePropertyListenerWithUserData

http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html

过期情话 2024-12-23 19:43:42

我刚刚遇到这个问题,但我正在使用 AVAudioPlayer。如果我尝试在调用播放后立即停用会话,则不起作用。但是等待audioPlayerDidFinishPlaying:成功:然后这样做就起作用了:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error;
BOOL wasSuccessful = [audioSession setActive:NO error:&error];
NSLog(@"wasSuccessful: %@", wasSuccessful ? @"Yes" : @"No");
}

我正在使用默认的音频会话,顺便说一句。

I just ran into this problem, but I'm using AVAudioPlayer. If I tried to deactivate my session right after calling play, it didn't work. But waiting for audioPlayerDidFinishPlaying:successfully: and then doing this worked:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error;
BOOL wasSuccessful = [audioSession setActive:NO error:&error];
NSLog(@"wasSuccessful: %@", wasSuccessful ? @"Yes" : @"No");
}

I'm using the default audio session, BTW.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文