如何以编程方式检测 iphone 中的耳机? [英] How to programmatically detect earpiece in iphone?

查看:31
本文介绍了如何以编程方式检测 iphone 中的耳机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开展一个项目,该项目涉及在应用程序内部播放 iphone 音乐库中的音乐.我正在使用 MPMediaPickerController 来允许用户选择他们的音乐并使用 iPhone 中的 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.

但是,当用户插入他的听筒并取下它时,我遇到了问题.音乐会突然无故停止播放.经过一些测试,我发现当用户从设备上拔下耳机时,iPod 播放器会暂停播放.那么有没有办法以编程方式检测耳机是否已拔下,以便我可以继续播放音乐?或者有什么办法可以防止iPod播放器在用户拔下耳机时暂停?

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?

推荐答案

您应该注册 AudioRoute 更改通知并实现您希望如何处理路由更改

You should register for AudioRoute changed notification and implement how you want to handle the rout changes

    // Registers the audio route change listener callback function
    AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange,
                                     audioRouteChangeListenerCallback,
                                     self);

并且在回调中,你可以得到路由改变的原因

and within the callback, you can get the reason for route change

  CFDictionaryRef   routeChangeDictionary = inPropertyValue;

  CFNumberRef routeChangeReasonRef =
  CFDictionaryGetValue (routeChangeDictionary,
            CFSTR (kAudioSession_AudioRouteChangeKey_Reason));

  SInt32 routeChangeReason;

      CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);

  if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) 
  {
       // Headset is unplugged..

  }
  if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
  {
       // Headset is plugged in..                   
  }

这篇关于如何以编程方式检测 iphone 中的耳机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆