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

查看:137
本文介绍了如何以编程方式检测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天全站免登陆