Detecting if headphones are plugged into iPhone

前端 未结 4 2086
余生分开走
余生分开走 2020-12-05 08:45

Does anyone know if you can detect if headphones are plugged into the iPhone, and if they aren\'t - disable sound from your application.

I think I could manage disab

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 09:11

    http://developer.apple.com/iphone/library/samplecode/SpeakHere/Introduction/Intro.html

    In this project there is a code-snippet where it pauses recording if the headphones is unpluged. Maybe you can use it to achieve your result.

    Good luck!

    (edit)

    You will have to study the SpeakHereController.mm file.
    I found this code in the awakeFromNib method

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

提交回复
热议问题