0

此代码在 iOS 8.0 设备中运行良好,但在 iOS 9.0 中始终将滑块值设为 0.000...

(void) volumeUp{

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

//find the volumeSlider
UISlider* volumeViewSlider = nil;
for (UIView *view in [volumeView subviews]){
    if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
        volumeViewSlider = (UISlider*)view; // volumeViewSlider always 0.000
        break;
    }
}

[volumeViewSlider setValue: [volumeViewSlider value] + 0.0625 animated:YES];
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];

}

我该如何解决?我需要每次将音量调高 0.0625。

谢谢!

4

1 回答 1

0

在您的代码中,当您调用 volumeUp 时,它会创建一个新的 MPVolumeView 并返回 0 或其他值。但是,如果您创建一个 MPVolumeView 并按属性保存它,它将提供您想要的。

像这样:

@interface ViewController ()
@property (strong, nonatomic) MPVolumeView *volumeView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    MPVolumeView *vv = [[MPVolumeView alloc] init];
    self.volumeView = vv;
}

- (IBAction)volumeUp:(id)sender
{
    //find the volumeSlider
    UISlider* volumeViewSlider = nil;
    for (UIView *view in [self.volumeView subviews]){
        if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
            volumeViewSlider = (UISlider*)view; // volumeViewSlider always 0.000
            break;
        }
    }

    [volumeViewSlider setValue: [volumeViewSlider value] + 0.0625 animated:YES];
    [volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
}
于 2016-02-05T00:52:28.523 回答