播放过程中拔掉的耳机会导致iPhone应用程序出错 [英] Headphones unplugged during playback cause bug on iPhone app

查看:144
本文介绍了播放过程中拔掉的耳机会导致iPhone应用程序出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于 Speak Here示例应用创建应用。我希望通过耳机播放音频,如果它们已插入或默认为扬声器。

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.

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

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.

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

 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");


推荐答案

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

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.

这篇关于播放过程中拔掉的耳机会导致iPhone应用程序出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆