iOS 7: Mpmusicplayercontroller Volume Deprecated. How to Change Device Volume Now

iOS 7: MPMusicPlayerController volume deprecated. How to change device volume now?

To answer you question exactly:
Yes there is other way to change system volume without user interaction.

Until recent times I used to think that changing volume using MPVolumeView programmatically is possible only using private API. But I have just verified, that changing the value of volumeSlider and faking slider's touchUP event works:

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;
break;
}
}

[volumeViewSlider setValue:1.0f animated:YES];
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];

(When slider receives touchUP event, it will invoke _commitVolumeChange method on itself, which will change the system volume)

iOS: Change Device Volume

Using iPodMusicPlayer would affect the actual iPod volume setting as well. If you want to avoid that, use this:

#import 
// ...
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
musicPlayer.volume = 1.0f;

As the user holex correctly mentioned the property volume in MPMusicPlayerController is deprecated in iOS 7.

Volume control with MPMusicPlayerController

These are the images I use - I've only included the 1x sizes - thumb is 54 x 30

[self.mpVolumeView setMinimumVolumeSliderImage:[UIImage imageNamed:@"master_volume_bar"] forState:UIControlStateNormal];
[self.mpVolumeView setMaximumVolumeSliderImage:[UIImage imageNamed:@"grey_volume_bar"] forState:UIControlStateNormal];
[self.mpVolumeView setVolumeThumbImage:[UIImage imageNamed:@"volume_thumb"] forState:UIControlStateNormal];

master_volume_bar

grey_volume_bar

volume_thumb

how to Control iOS system Volume using a UISlider

I love new learners... Simple answer for you...

First create a UIView in your StoryBoard, where you want to put a volume slider.
Now create a IBOutlet for that view in your ViewController implementation.

for ex:

@property (nonatomic,strong) IBOutlet UIView* volumeView;

Now in the viewDidLoad method write the following code:

MPVolumeView *mpVolumeView = [[MPVolumeView alloc]initWithFrame:self.volumeView.bounds];
[self.volumeView addSubview:mpVolumeView];
[mpVolumeView sizeToFit];

Now you will have volume control in your app...

Note: this code will not work for a simulator.

cheers.



Related Topics



Leave a reply



Submit