美文网首页
iOS开发 监听设备音量变化

iOS开发 监听设备音量变化

作者: AR24 | 来源:发表于2020-10-21 09:53 被阅读0次

在想要添加监听设备音量变化的地方加入下面两句代码

//监听音量变化的系统通知
selector:@selector(systemVolumeDidChangeNoti:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
//开始接收遥控事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
 /*必须用通知里面的userInfo键值对中的AVSystemController_AudioVolumeNotificationParameter
只有这里面的值才是最新的音量值,如果还是用 [AVAudioSession sharedInstance].outputVolume,得到的还是旧的音量值(它们需要过一会才会被设成新值)*/
-(void)systemVolumeDidChangeNoti:(NSNotification* )noti{
    //目前手机音量
    if ([noti.name isEqualToString:@"AVSystemController_SystemVolumeDidChangeNotification"]) {
        NSDictionary *dic = noti.userInfo;
        self.circleView.progress = [dic[@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
    }
}

相关文章

网友评论

      本文标题:iOS开发 监听设备音量变化

      本文链接:https://www.haomeiwen.com/subject/gdmkmktx.html