ios 视频播放-获取与设置音量
2020-02-24 本文已影响0人
愤斗的小蚂蚁
获取音量
通过MPVolumeView的子视图,私有方法获取MPVolumeSlider,获取音量。有风险,不推荐使用。
#import <MediaPlayer/MediaPlayer.h>
MPVolumeView *volumeView = [MPVolumeView new];
volumeView.showsRouteButton = NO;
volumeView.showsVolumeSlider = NO;
[self.view addSubview:volumeView];
__block UISlider *volumeViewSlider = nil;
[[volumeView subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[UISlider class]]) {// 私有的方法 ,不推荐
volumeViewSlider = obj;
*stop = YES;
}
}];
CGFloat volume = volumeViewSlider.value;// 0, 不是当前音量
通过私用方法获取到控制音量的slider,只需要监听它值的改变(valueChange)便可以监听到系统音量的改变,但是在MPVolumeView被创建的时候,其中的slider的值还是为0,需要过一会时间系统才会给slider赋值(不止会赋值一次),此时会调用valueChange,但是并不意味这系统音量的改变。同理,如果想获取系统音量,用AVAudioSession.sharedInstance().outputVolume这个方法,直接读slider的值可能会读不准
监听方式 如下
KVO方式 待续、、、
#import <AVFoundation/AVFoundation.h>
- (void) someWhere {
// part 1.0 与 part 2.0 执行顺序无关,可以写在其他位置
// part 1.0
//监听系统声音
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryAmbient error:nil];//重点方法
[session setActive:YES error:nil];
NSError *error;
[[AVAudioSession sharedInstance] setActive:YES error:&error];
//注,ios9上不加这一句会无效
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// part 2.0
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
}
- (void) volumeChanged:(NSNotification *)notification {
float volume = [[[notification userInfo] objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
ZHNSLog(@"自定义 音量 = %.5f", volume);
}
swift代码如下
let volumeV = MPVolumeView(frame: CGRect(x: -3, y: -3, width: 1, height: 1))
for view in volumeV.subviews{
if view is UISlider{
self.volumeSlider = (view as! UISlider)
}
}
self.view.addSubview(volumeV)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.volumeChange(_:)) , name:Notification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification") , object: nil)
@objc func volumeChange(_ notification:NSNotification) {
let userInfo = notification.userInfo!
let volume = userInfo["AVSystemController_AudioVolumeNotificationParameter"] as! Double
}
}
设置音量
1-通过MPMusicPlayerController的volume可以设置音量,查看文档该属性ios(3.0, 7.0),使用时有⚠️。在ios12.0+,13.0+测试可以使用,代码设置音量有效
2-通过MPVolumeView的子视图,私有方法获取MPVolumeSlider,有风险,不推荐使用。在ios12.0+,13.0+测试无效。
#import <MediaPlayer/MediaPlayer.h>
- (void) settingDeviceVolume {
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
// ios12.0+,13.0+测试可以使用,代码设置音量有效
if ( [musicPlayer respondsToSelector:@selector(setVolume:)] ) {
//消除警告
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[musicPlayer setVolume:0.34512];//MP_DEPRECATED("Use MPVolumeView for volume control.", ios(3.0, 7.0));
#pragma clang diagnostic pop
}
else {
MPVolumeView *volumeView = [MPVolumeView new];
volumeView.frame=CGRectMake(10,200, 200, 30);
volumeView.backgroundColor = [UIColor redColor];
volumeView.showsRouteButton = YES;
volumeView.showsVolumeSlider = YES;
[self.view addSubview:volumeView];
__block UISlider *volumeViewSlider = nil;
[[volumeView subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[UISlider class]]) {// 私有的方法 ,不推荐
volumeViewSlider = obj;
*stop = YES;
}
// 或
if ([((UIView *)obj).class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider = (UISlider*)obj;
*stop = YES;
}
}];
volumeViewSlider.backgroundColor = [UIColor blueColor];
[volumeViewSlider setValue:0.27896 animated:YES];// ios12.0+,13.0+测试无效
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}