• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

以编程方式检测 iphone 的耳机?

用户头像
it1352
帮助1

问题说明

我目前正在开展一个项目,该项目涉及在应用程序内部播放 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?

正确答案

#1

您应该注册 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..                   
  }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfkefk
系列文章
更多 icon
同类精品
更多 icon
继续加载