zoukankan      html  css  js  c++  java
  • IOS 判断耳机插入/拔出

    一. 方式

     1.注册监听

    //注册监听耳机设备的插入/拔出

        AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange,audioRouteChangeListenerCallback,  nil);

    2.

    void audioRouteChangeListenerCallback (

    void                      *inUserData,

    AudioSessionPropertyID    inPropertyID,

    UInt32                    inPropertyValueSize,

    const void                *inPropertyValue

                                           ) {

    if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;

    CFDictionaryRef routeChangeDictionary = (CFDictionaryRef)inPropertyValue;

    CFNumberRef routeChangeReasonRef =

        (CFNumberRef)CFDictionaryGetValue (routeChangeDictionary,

                              CFSTR (kAudioSession_AudioRouteChangeKey_Reason));

    SInt32 routeChangeReason;

    CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);

    if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {

    NSLog(@"拔掉耳机");

        } elseif (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable) {

    NSLog(@"插入耳机");

        } elseif (routeChangeReason == kAudioSessionRouteChangeReason_NoSuitableRouteForCategory) {

        }

    }

    二 . 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outputDeviceChanged:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];

     

    - (void)outputDeviceChanged:(NSNotification *)aNotification

    {

        BOOL flag = [AudioHelper hasHeadset];

        if (flag) {

            [MBProgressHUD showMessageHUDAddedTo:self.view message:@"设备已插入"];

        }

        else

        {

            [MBProgressHUD showMessageHUDAddedTo:self.view message:@"设备已拔出"];

        }

    }

     

  • 相关阅读:
    进程隐藏类
    MFC中一些使用的方法
    c++/vc++的一些学习方法
    随意更改桌面壁纸
    Winsock异步模型之(事件通知模型 WSAAsyncSelect)
    开发外挂的一些原理
    ios 图片
    得到cell的坐标
    取出字符串中的空格
    ios 获取当前的日期,年月日
  • 原文地址:https://www.cnblogs.com/fangkuai/p/4241616.html
Copyright © 2011-2022 走看看