iPad 处理不当路线改变扬声器 ->耳机

发布于 2024-11-03 16:55:16 字数 1794 浏览 1 评论 0原文

我运行的是 iOS 4.3.2。

  1. 我在 iPad 上启动应用程序(未插入任何电源)。
  2. 音频作品。
  3. 我插上耳机。
  4. iPad 和耳机都没有声音。
  5. 我拔下耳机,转到 (2)

或者:

  1. 我在插入耳机的情况下在 iPad 上启动我的应用程序。
  2. 音频从耳机中发出。
  3. 我拔掉耳机。
  4. 现在音频从 iPad 中发出。
  5. 我插上耳机。
  6. iPad 和耳机都没有声音。
  7. 我再次拔下耳机,转到 (4)。

这是我的音频会话代码:

@implementation AudioSession

+ (void) CreateAudioSessionWithInterruptionListener: (AudioSessionInterruptionListener) interruptionListener 
                                returningSampleRate: (Float64 *) pHWSampleRate 
{
    OSStatus result = AudioSessionInitialize(NULL, NULL, interruptionListener, self);
    if (result != kAudioSessionNoError)
    {
        NSLog(@"AudioSessionInitialize(...) failed!");
        return;
    }

    SET_PROPERTY( kAudioSessionProperty_AudioCategory,                        UInt32,   kAudioSessionCategory_PlayAndRecord );
    SET_PROPERTY( kAudioSessionProperty_OverrideCategoryMixWithOthers,        UInt32,   (UInt32) YES                        );
    SET_PROPERTY( kAudioSessionProperty_PreferredHardwareIOBufferDuration,    Float32,  .005                                );

    // GET not set h/w sampleRate
    // Float64 hwSampleRate;
    // UInt32 size = sizeof(Float64);
    AssertOK(AudioSessionGetProperty(
                                     kAudioSessionProperty_CurrentHardwareSampleRate, 
                                     & (UInt32) { sizeof(Float64) }, 
                                     pHWSampleRate), 
             @"couldn't get hw sample rate");
    //NSAssert(size == sizeof(Float64);
    NSLog(@"H/W SampleRate: %d", (int) (* pHWSampleRate));

    AssertOK(AudioSessionSetActive(true), 
             @"couldn't set audio session active\n");
}

I'm running iOS 4.3.2.

  1. I start my app on my iPad (nothing plugged in).
  2. Audio works.
  3. I plug in the headphones.
  4. No audio, neither from the iPad, nor from the headphones.
  5. I unplug the headphones, go to (2)

Alternatively:

  1. I start my app on my iPad with the headphones plugged in.
  2. Audio comes out of the headphones.
  3. I unplug the headphones.
  4. Now audio comes out of the iPad.
  5. I plug in the headphones.
  6. No audio, neither from the iPad, nor from the headphones.
  7. I unlpug the headphones again, goto (4).

Here is my audio session code:

@implementation AudioSession

+ (void) CreateAudioSessionWithInterruptionListener: (AudioSessionInterruptionListener) interruptionListener 
                                returningSampleRate: (Float64 *) pHWSampleRate 
{
    OSStatus result = AudioSessionInitialize(NULL, NULL, interruptionListener, self);
    if (result != kAudioSessionNoError)
    {
        NSLog(@"AudioSessionInitialize(...) failed!");
        return;
    }

    SET_PROPERTY( kAudioSessionProperty_AudioCategory,                        UInt32,   kAudioSessionCategory_PlayAndRecord );
    SET_PROPERTY( kAudioSessionProperty_OverrideCategoryMixWithOthers,        UInt32,   (UInt32) YES                        );
    SET_PROPERTY( kAudioSessionProperty_PreferredHardwareIOBufferDuration,    Float32,  .005                                );

    // GET not set h/w sampleRate
    // Float64 hwSampleRate;
    // UInt32 size = sizeof(Float64);
    AssertOK(AudioSessionGetProperty(
                                     kAudioSessionProperty_CurrentHardwareSampleRate, 
                                     & (UInt32) { sizeof(Float64) }, 
                                     pHWSampleRate), 
             @"couldn't get hw sample rate");
    //NSAssert(size == sizeof(Float64);
    NSLog(@"H/W SampleRate: %d", (int) (* pHWSampleRate));

    AssertOK(AudioSessionSetActive(true), 
             @"couldn't set audio session active\n");
}

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

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

发布评论

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

评论(1

情绪失控 2024-11-10 16:55:16

事实上这可能是一个 iOS 错误。切换到 HDMI 输出(也是 iOS 4.3)时我遇到了同样的问题。在 50% 的情况下,路线更改会成功,并且音频会像预期那样从电视而不是 iPad 中发出。在另外 50% 的情况下,插入 HDMI 适配器后,我根本没有音频,无论是电视还是 iPad。再次拔下适配器后,我会立即恢复音频。

所以我不能真正为您提供一个好的解决方案,只能提供一个解决方案。注册音频路由更改(通过为属性 kAudioSessionProperty_AudioRouteChange 设置属性侦听器)。当此回调触发时,请处理它,例如通过重新初始化所有核心音频内容。这不是一个非常好的解决方案,但却是一个有效的解决方案。

更新:
在您的特殊情况下,系统的行为几乎与苹果所说的一样。让我引用 Apple 的 iOS 界面指南:

播放的情况类似,但结果不同,如图所示
在图的右侧。如果用户在使用过程中拔掉耳机
播放时,您的回调应该暂停音频。如果用户插入
在播放期间使用耳机,您的回调应该简单地允许播放
继续。

所以苹果说:从内置扬声器切换到耳机 ->播放继续。从耳机切换到内置扬声器 ->播放暂停。根据这句话,这是一种您应该自己实现的行为,而不是系统为您强制执行的行为,但它非常接近您实际看到的行为,不是吗?

This might be an iOS bug in fact. I have the same issue when switching to HDMI output (also iOS 4.3). In 50% of all cases, the route change succeeds and audio comes out of the television instead of the iPad, as expected. In the other 50% of all cases, after plugging in the HDMI adapter, I have no audio at all, neither television, nor iPad. I get audio back as soon as I unlpug the adapter again.

So I cannot really offer you a nice solution, only a work-a-round. Register for audio route changes (by setting a property listener for the property kAudioSessionProperty_AudioRouteChange). When this callback fires, handle it, e.g. by re-initializing all your Core Audio stuff. This is not a very nice solution, but one that works.

Update:
In your special case, the system behaves pretty much how Apple says it shall behave. Let me quote from Apple's Interface Guidelines for iOS:

The case for playback is similar but has different outcomes, as shown
on the right of the figure. If a user unplugs the headset during
playback, your callback should pause the audio. If a user plugs in the
headset during playback, your callback should simply allow playback to
continue.

So Apple says: Switching from internal speaker to headphone -> playback continues. Switching from headphone to internal speaker -> playback pauses. According to this quote this is a behavior you should implement on your own, not one that the system enforces for you, but it is pretty close to the behavior you are actually seeing, isn't it?

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