2016-02-04 70 views
0

此代碼的iOS 8.0設備工作正常,但在iOS的9.0總是讓滑塊值0.000 ...如何更改iOS9中的音量?

(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。

謝謝!

+0

是volumeViewSlider也許零for循環後? – onnoweb

回答

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]; 
} 
相關問題