I'm trying to find a way to detect the plugged/unplugged event on headphone Jack in Xamarin.iOS, specifically on latest version (iOS 7). I've found posts that give solution for earlier versions, but that doesn't seems to work for iOS 7. Is there a way to do it in this version? and if it is, how can I implement that functionality?

有帮助吗?

解决方案 2

    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                        
            }
    }

其他提示

Something like this:

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;
}

Didn't try it on a real device. Just convert one of Objective-C snippets.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top