سؤال

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