介绍:整理的两种设置播放器播放静音的方法
一、MPVolumeView-设置音量值为0
1.获取到当前系统音量值(恢复音量时有用)
volumeFloat = [self getVolume]; -(float) getVolume { //获取系统系统音量视图 MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: CGRectMake(-1000, -100, 100, 100)]; volumeView.hidden = NO;//不显示系统音量调节视图 [self addSubview:volumeView]; volumeViewSlider = nil; for(UIView *view in [volumeView subviews]) { if([[[view class] description] isEqualToString:@"MPVolumeSlider"]) { volumeViewSlider=(UISlider *)view; break; } } float val = [volumeViewSlider value]; return val; }
2.关闭系统音量(设置音量值为0)
//关闭系统音量(静音) -(void)closeSysVolume { [volumeViewSlider setValue:0 animated:NO]; }
3.恢复原来的音量
-(void)reStartSysVolume { NSLog(@"重启音量"); if (volumeFloat == 0) { volumeFloat = 0.5; } [volumeViewSlider setValue:volumeFloat animated:NO]; //注:设置恢复后不会影响系统音量图标的正常显示 MPVolumeView *volumeView = [MPVolumeView new]; volumeView.hidden = YES; }
二、AVAudioSession
1.静音
//关闭系统音量(静音) -(void)closeSysVolume { [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil]; }
2.重新开启音量(注:调用closeSysVolume后使用该方法无效--还没找到恢复音量方法)
-(void)reStartSysVolume { NSLog(@"重启音量"); [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil]; }