문제

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