检测耳机是否插入 iPhone [英] Detecting if headphones are plugged into iPhone
问题描述
有谁知道您是否可以检测到耳机是否已插入 iPhone,如果没有 - 禁用应用程序中的声音.
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 disabling sound, but the detection part I have yet to find anything on.
谢谢
推荐答案
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.
祝你好运!
(编辑)
您必须研究 SpeakHereController.mm 文件.
我在 awakeFromNib
方法
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
", 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
", error);
这篇关于检测耳机是否插入 iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!