问题描述
- 系统声音使用MPVolumeView在8.4版本可以获取到系统音量的值,但在9.0的时候就获取不到
-
直接上代码:
MPVolumeView volumeView = [[MPVolumeView alloc] init];
UISlider volumeViewSlider = nil;
volumeView.frame = CGRectMake(-1000, -100, 100, 100);
volumeView.hidden = NO;
for (UIView view in [volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider = (UISlider)view;
break;
}
}// retrieve system volume
// float systemVolume2 = volumeViewSlider.value;
NSLog(@"视频当前音量:%f",systemVolum2);
// change system volume, the value is between 0.0f and 1.0f
[volumeViewSlider setValue:systemVolum2 animated:NO];// send UI control event to make the change effect right now. [volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
解决方案
解决方案二:
有可能是volumeViewSlider 还未创建,
我的解决方案是 判断volumeViewSlider 不存在 或者 音量为0时,就延时2秒后再次获取
时间: 2024-09-13 01:27:57