如何以编程方式检测 iPhone 中的耳机?
我目前正在开发一个项目,该项目涉及在应用程序内部播放 iPhone 音乐库中的音乐。我使用 MPMediaPickerController 允许用户选择他们的音乐并使用 iPhone 中的 iPod 音乐播放器播放它。
但是,当用户插入耳机并将其取下时,我遇到了问题。音乐会无缘无故地突然停止播放。经过一些测试,我发现当用户从设备上拔下耳机时,iPod 播放器将暂停播放。那么有没有什么方法可以以编程方式检测耳机是否已拔出,以便我可以继续播放音乐?或者有什么方法可以防止 iPod 播放器在用户拔下耳机时暂停?
I'm currently working on a project that involves playing music from the iphone music library within the app inside. I'm using MPMediaPickerController to allow the user to select their music and play it using the iPod music player within the iPhone.
However, i ran into problem when the user insert his earpiece and removes it. The music will suddenly stop playing for no reason. After some testing, i found out that the iPod player will pause playing when the user unplug his earpiece from the device. So is there any way to programatically detect if the earpiece has been unplug so that i can resume playing the music? Or is there any way to prevent iPod player from pausing when the user unplug his earpiece?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该注册 AudioRoute 更改通知并实现您想要处理路由更改的方式
,并且在回调中,您可以获取路由更改的原因
You should register for AudioRoute changed notification and implement how you want to handle the rout changes
and within the callback, you can get the reason for route change
如果您只想检查耳机是否在任何给定时间插入,而不监听路由变化,您可以简单地执行以下操作:
干杯,
拉斐尔·科拉桑特
If you just want to check whether headphones are plugged in at any given time, without listening to route changes, you can simply do the following:
Cheers,
Raffaello Colasante
我看到您正在使用 MPMediaPlayer 框架,但是麦克风处理是使用 AVAudioPlayer 框架完成的,您需要将其添加到您的项目中。
Apple 的网站上有来自 AVAudioPlayer 框架的代码,我用它来处理用户插入或移除 Apple 麦克风耳机造成的中断。
查看苹果的 iPhone 开发中心音频会话编程指南。
我的代码有点不同,其中一些可能会对您有所帮助:
I see you are using the MPMediaPlayer Framework however the microphone handling is done using the AVAudioPlayer framework, which you will need to add to your project.
Apple's website has code from the AVAudioPlayer framework which I use to handle interruptions from a user plugging in or removing the Apple microphone headphones.
Check out Apple's iPhone Dev Center Audio Session Programming Guide.
My code is a little different and some of this may help you:
大家好,只需查看 AddMusic 示例应用程序即可。将解决您所有与 iPod 相关的问题
首先使用以下代码注册 iPod 播放器以获取通知
并在通知中实现以下代码
Hey guys just check AddMusic sample app. Will solve all your iPod related issues
First register iPod player for notification with following code
And implement the following code in the notification