How to change volume programmatically on iOS 11.4

前端 未结 4 1106
深忆病人
深忆病人 2021-01-17 18:52

Before, I was setting sound volume programmatically using this approach:

MPVolumeView *volumeView = [[MPVolumeView alloc] init];
UISlider *volumeViewSlider =         


        
4条回答
  •  心在旅途
    2021-01-17 19:36

    Changing volumeViewSlider.value after a small delay resolves problem.

    - (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;
      });
    }
    

    Swift version

提交回复
热议问题