您的位置:首页 > 移动开发 > IOS开发

多媒体音量控制

2015-05-26 00:00 1011 查看
方案一:

MPMusicPlayerController

// This property is deprecated -- use MPVolumeView for volume control instead.
该方法在7.0后已经标记为废除

获取音量

[MPMusicPlayerController applicationMusicPlayer].volume

设置音量

[[MPMusicPlayerController applicationMusicPlayer] setVolume:newVolume];

使用该方法改变音量时,会出现系统的音量调节的View, 如果要隐藏,需要实例化一个MPVolumeView,然后将其的加入当前的view中,并将其的位置设置在不可见的地方(可以是超出屏幕的位置,注意使用hidden是无效的)。

方案二:
MPVolumeView (需要导入<MediaPlayer/MPVolumeView.h>)
注意:需要使用真机,模拟器无法看到

MPVolumeView m_volumeView = [[MPVolumeView alloc]initWithFrame:CGRectMake(10, 10, 200, 30)];
[self.view addSubview:m_volumeView];

调节滑动条会自动修改系统音量。

监听系统音量变化
使用通知:AVSystemController_SystemVolumeDidChangeNotification 对系统的音量进行监听。

注册通知

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

通知回调处理
-(void) volumeChanged:(NSNotification *)notification
{
float volume = [[[notification userInfo] objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
if(volume == m_currentVolume)
{
return;
}
m_currentVolume = volume;

// TODO
}

删除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  IOS 多媒体 音量