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;

        

        

        

        

        

     

    }

  • 相关阅读:
    jqgrid addRowData报错
    LINE 项目总结
    bootstrap-table 常用总结-树形结构(展开和折叠)
    bootstrap-table 常用总结-树形结构
    为wordpress的分类以及子分类指定固定模版
    wordpress主题开发-部分函数调用整理
    superslide滚动插件使用记录-产品滚动-图片滚动
    4GL之Non-SCROLLING CURSOR
    asp.net报表结构学习记录
    HTML基础教程_1
  • 原文地址:https://www.cnblogs.com/pocket-mood/p/4506546.html
Copyright © 2011-2022 走看看