zoukankan      html  css  js  c++  java
  • iOS 声音按键监听和实现

    首先包含这两个头文件以及加入对应的框架

    #import <MediaPlayer/MediaPlayer.h>

    #import <AudioToolbox/AudioToolbox.h>

     

    添加声音通知的监听

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];

     

    // 获取当前系统音量

     

        self.volume = [[MPMusicPlayerController applicationMusicPlayer] volume];

     

    // 判断是否静音

     

     

     

    - (BOOL) isMuted

     

    {

        

        CFStringRef route;

        

        UInt32 routeSize = sizeof(CFStringRef);

        

        

        

        OSStatus status = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &routeSize, &route);

        

        if (status == kAudioSessionNoError)

            

        {

            

            if (route == NULL || !CFStringGetLength(route))

                

                return TRUE;

            

        }

        

        

        

        return FALSE;

        

     

    }

     

     

    // 声音通知监听的实现

    - (void) volumeChanged:(NSNotification *) notification

     

    {

        // 获取当前声音的值

        float volume =

        

        [[[notification userInfo]

          

          objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]

         

         floatValue];

    //    NSLog(@"现在音量:%f",volume);

        if (!self.isMuted && volume < self.volume && self.acceptBtn.hidden == NO) {

            // 设置音量为静音

            [[MPMusicPlayerController applicationMusicPlayer] setVolume:0.0];

           // 添加系统震动

            AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);

            NSLog(@"----isMuted音量:%f-------",volume);

            

        } else if (volume > self.volume && self.acceptBtn.hidden == NO){

            

            [[MPMusicPlayerController applicationMusicPlayer] setVolume:volume];

            NSLog(@"----!isMuted音量:%f-------",volume);

            

        }

        

        // 更新记录音量值的变量

        self.volume = volume;

        

        

        

        

        

     

    }

  • 相关阅读:
    poj 3277 City Horizon (线段树 扫描线 矩形面积并)
    HDU 1255 覆盖的面积 (扫描线 线段树 离散化 矩形面积并)
    Codeforces Round #260 (Div. 2)
    poj 1151 Atlantis (离散化 + 扫描线 + 线段树 矩形面积并)
    CF1237F Balanced Domino Placements
    CF954H Path Counting
    AT2395 [ARC071C] TrBBnsformBBtion
    AT2400 [ARC072B] Alice&Brown
    AT2401 [ARC072C] Alice in linear land
    [国家集训队]阿狸和桃子的游戏
  • 原文地址:https://www.cnblogs.com/pocket-mood/p/4506546.html
Copyright © 2011-2022 走看看