需求分析:
观看视频时,点击音量按键,屏蔽系统音量弹窗,显示自定义的音量控件,提高应用观看体验
步骤:
1.创建MPVolumeView
MPVolumeView *volumeView = [[MPVolumeView alloc]initWithFrame:CGRectMake(-100, -100, 10, 10)];
[self.view addSubview:volumeView];
2.设置AVAudioSession
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
if (audioSession.otherAudioPlaying) {
//有其他音乐在后台进行播放时,显示系统控件
[audioSession setActive:NO error:&error];
} else {
//自己的应用会走这一步
[audioSession setActive:YES error:&error];
}
3.监听音量的改变
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
4.根据音量改变,自定义音量显示UI
- (void)volumeChanged:(NSNotification *)notification
{
float volume =
[[[notification userInfo]
objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
floatValue];
NSLog(@"当前的音量 = %f", volume);
}
网友评论