Headphones unplugged during playback cause bug on

2019-04-14 20:04发布

问题:

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

回答1:

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.



回答2:

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