3

我正在尝试找到一种方法来检测 Xamarin.iOS 中耳机插孔上的插入/拔出事件,特别是在最新版本(iOS 7)上。我找到了为早期版本提供解决方案的帖子,但这似乎不适用于 iOS 7。有没有办法在这个版本中做到这一点?如果是,我该如何实现该功能?

4

2 回答 2

2

像这样的东西:

using System.Runtime.InteropServices;
using MonoTouch.AudioToolbox;

...

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();
    AudioSession.AddListener(AudioSessionProperty.AudioRouteChange, EventHandler);  
}

void EventHandler(AudioSessionProperty prop, int size, IntPtr data)
{
    var isHeadphonesAvailable = IsHeadphonesAvailable (prop, size, data);
    if (isHeadphonesAvailable) {
        // Do something
    } else {
        // Do something else
    }
}

bool IsHeadphonesAvailable(AudioSessionProperty prop, int size, IntPtr data)
{
    var result = false;
    if (prop == AudioSessionProperty.AudioRouteChange) {
    var text = Marshal.PtrToStringAuto (data, size);
    result = (text.Contains ("Head"));
    }
    return result;
}

没在真机上试过。只需转换其中一个Objective-C片段。

于 2013-11-02T18:08:38.087 回答
1
    using System.Runtime.InteropServices;
    using MonoTouch.AudioToolbox;

    public override void ViewDidLoad()
    {
      base.ViewDidLoad();
      .
      .
      .
      AudioSession.Initialize(null, NSRunLoop.NSDefaultRunLoopMode);
      AudioSession.AudioRouteChanged += AudioSession_AudioRouteChanged;
    }

    void AudioSession_AudioRouteChanged (object sender, AudioSessionRouteChangeEventArgs e)
    {
            if (e.CurrentOutputRoutes [0] == AudioSessionOutputRouteKind.Headphones) 
            {
                    //Code when is plugged
            }
            else
            {
                    //Code when is unplugged                        
            }
    }
于 2013-11-12T19:10:51.013 回答