以前,我使用这种方法以编程方式设置音量:
MPVolumeView *volumeView = [[MPVolumeView alloc] init]; UiSlider *volumeViewSlider = nil; for (UIView *view in [volumeView subviews]) { if ([view.class.description isEqualToString:@"MPVolumeSlider"]) { volumeViewSlider = (UiSlider *)view; break; } } [volumeViewSlider setValue:0.5 animated:YES]; [volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
直到iOS 11.4它运行良好(即使在iOS 11.3上),但在iOS 11.4上却没有.体积值保持不变.有人可以帮助解决这个问题吗?谢谢.
解决方法
在一小段延迟后更改volumeViewSlider.value可以解决问题.
- (IBAction)increase:(id)sender { MPVolumeView *volumeView = [[MPVolumeView alloc] init]; UiSlider *volumeViewSlider = nil; for (UIView *view in volumeView.subviews) { if ([view isKindOfClass:[UiSlider class]]) { volumeViewSlider = (UiSlider *)view; break; } } dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(0.01 * NSEC_PER_SEC)),dispatch_get_main_queue(),^{ volumeViewSlider.value = 0.5f; }); }