播放时拔掉耳机会导致 iPhone 应用程序出现错误
我正在创建一个基于在这里发言示例应用的应用程序。如果耳机已插入或默认通过扬声器播放,我希望音频通过耳机播放。
我使用了底部的代码来实现这一点,并且除非在播放过程中拔掉耳机,否则它工作正常。此时播放结束,没关系。问题是,当我再次点击播放时,播放变得很奇怪,并且停止按钮停止工作。它还从停止的地方开始播放,而不是像通常按下停止按钮时那样从头开始重置。
同样,如果在应用程序打开之前插入耳机,也会导致奇怪的行为。
也许我需要抓住“耳机拔出”事件并让它“按下停止按钮”?因为现在的做法是不正确的。
用简单的形式表达我的问题:如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过进一步调查,我现在意识到我在这里实质上问了两个问题。到目前为止我已经解决了一个。
有时,当播放中断时,它是“暂停”而不是“停止”。我刚刚删除了执行此“暂停”操作的代码中的任何元素,并将其替换为“停止”音频并回滚其队列的调用。
至于音频是从扬声器而不是耳机发出的,我仍在研究这一点。
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.
您是否尝试过实现 AVAudioSessionDelegate 协议,该协议应该在拔掉耳机之类的东西时触发中断委托方法。 此处的文档。
Have you tried implementing the AVAudioSessionDelegate protocol that should fire the interrupt delegate methods when things like the headphones are unplugged. Documentation here.