播放时拔掉耳机会导致 iPhone 应用程序出现错误

发布于 2024-10-21 14:50:14 字数 2039 浏览 1 评论 0原文

我正在创建一个基于在这里发言示例应用的应用程序。如果耳机已插入或默认通过扬声器播放,我希望音频通过耳机播放。

我使用了底部的代码来实现这一点,并且除非在播放过程中拔掉耳机,否则它工作正常。此时播放结束,没关系。问题是,当我再次点击播放时,播放变得很奇怪,并且停止按钮停止工作。它还从停止的地方开始播放,而不是像通常按下停止按钮时那样从头开始重置。

同样,如果在应用程序打开之前插入耳机,也会导致奇怪的行为。

也许我需要抓住“耳机拔出”事件并让它“按下停止按钮”?因为现在的做法是不正确的。

用简单的形式表达我的问题:如何在 iPhone 上正确设置核心音频以通过扬声器和耳机播放。

任何可以解决此问题的代码都会很棒帮助。谢谢!

 OSStatus error = AudioSessionInitialize(NULL, NULL, NULL, NULL);

if (error) printf("ERROR INITIALIZING AUDIO SESSION! %d\n", error);
else 
{
    UInt32 category = kAudioSessionCategory_PlayAndRecord;  
    // UInt32 category = kAudioSessionCategory_MediaPlayback;   

    error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
    if (error) printf("couldn't set audio category!");


    // It is bugs when I unplug the headphones! 
    UInt32 doChangeDefaultRoute = 1;        
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);




    error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, propListener, self);
    if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", error);
    UInt32 inputAvailable = 0;
    UInt32 size = sizeof(inputAvailable);

    // we do not want to allow recording if input is not available
    error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
    if (error) printf("ERROR GETTING INPUT AVAILABILITY! %d\n", error);
    btn_record.enabled = (inputAvailable) ? YES : NO;

    // we also need to listen to see if input availability changes
    error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, propListener, self);
    if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", error);

    error = AudioSessionSetActive(true); 
    if (error) printf("AudioSessionSetActive (true) failed");

I am creating an application based on the Speak Here example app. I want the audio to play through the headphones if they are plugged in or otherwise default to the speakers.

I've used the bottom bit of code to make this happen and it works fine unless the headphones are unplugged during playback. At that point the playback ends, which is okay. The problem is when I hit play again the playback comes out all weird and the stop button stops working. It also starts playback from where it left off rather then resetting from the beginning as it normally does when you hit the stop button.

Likewise if the headphones are plugged in before the app opens this also results in strange behaviour.

Maybe I need to grab the 'headphones unplugged' event and get it to 'press the stop button'? Because the way it is doing it now is not correct.

To put my question in a simple form: How do you properly setup core audio on the iPhone for playback through the speakers and headphones.

Any pieces of code that could solve this problem would be of great help. Thanks!

 OSStatus error = AudioSessionInitialize(NULL, NULL, NULL, NULL);

if (error) printf("ERROR INITIALIZING AUDIO SESSION! %d\n", error);
else 
{
    UInt32 category = kAudioSessionCategory_PlayAndRecord;  
    // UInt32 category = kAudioSessionCategory_MediaPlayback;   

    error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
    if (error) printf("couldn't set audio category!");


    // It is bugs when I unplug the headphones! 
    UInt32 doChangeDefaultRoute = 1;        
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);




    error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, propListener, self);
    if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", error);
    UInt32 inputAvailable = 0;
    UInt32 size = sizeof(inputAvailable);

    // we do not want to allow recording if input is not available
    error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
    if (error) printf("ERROR GETTING INPUT AVAILABILITY! %d\n", error);
    btn_record.enabled = (inputAvailable) ? YES : NO;

    // we also need to listen to see if input availability changes
    error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, propListener, self);
    if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", error);

    error = AudioSessionSetActive(true); 
    if (error) printf("AudioSessionSetActive (true) failed");

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

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

发布评论

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

评论(2

£噩梦荏苒 2024-10-28 14:50:14

经过进一步调查,我现在意识到我在这里实质上问了两个问题。到目前为止我已经解决了一个。

有时,当播放中断时,它是“暂停”而不是“停止”。我刚刚删除了执行此“暂停”操作的代码中的任何元素,并将其替换为“停止”音频并回滚其队列的调用。

至于音频是从扬声器而不是耳机发出的,我仍在研究这一点。

After further investigation I realize now that I essentially asked two questions here. I've solved one so far.

Sometimes when the playback is interrupted it is 'paused' rather than 'stopped'. I just eliminated any elements of the code that did this 'pausing' and replaced it with calls to 'stop' the audio and wind back it's queue.

As for the audio coming out of the speakers instead of the headphones, I'm still looking into that one.

就像说晚安 2024-10-28 14:50:14

您是否尝试过实现 AVAudioSessionDelegate 协议,该协议应该在拔掉耳机之类的东西时触发中断委托方法。 此处的文档。

Have you tried implementing the AVAudioSessionDelegate protocol that should fire the interrupt delegate methods when things like the headphones are unplugged. Documentation here.

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