2

I'm writing an app for ios7 that needs to control the volume of the built in music player or the master volume level in general. I've tried using the MediaPlayer.framework but not sure if that's right. I'm wondering what the best way to go about this is. Thanks!

user3367709
  • 21
  • 1
  • 2

3 Answers3

2

According to Apple, in iOS7 setting MPMusicPlayerController's volume is deprecated:

// The current volume of playing music, in the range of 0.0 to 1.0.
// This property is deprecated -- use MPVolumeView for volume control instead.
@property(nonatomic) float volume NS_DEPRECATED_IOS(3_0, 7_0);

Apple recommends using MPVolumeView, so I came up with this:

Add volumeSlider property:

@property (nonatomic, strong) UISlider *volumeSlider;

Init MPVolumeView and add somewhere to your view (can be hidden, without frame, or empty because of showsRouteButton = NO and showsVolumeSlider = NO):

MPVolumeView *volumeView = [MPVolumeView new];
volumeView.showsRouteButton = NO;
volumeView.showsVolumeSlider = NO;
[self.view addSubview:volumeView];

Find and save reference to UISlider:

__weak __typeof(self)weakSelf = self;
[[volumeView subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[UISlider class]]) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.volumeSlider = obj;
        *stop = YES;
    }
}];

Add target action for UIControlEventValueChanged:

[self.volumeSlider addTarget:self action:@selector(handleVolumeChanged:) forControlEvents:UIControlEventValueChanged];

And then detect volume changing (i.e. by the hardware volume controls):

- (void)handleVolumeChanged:(id)sender
{
    NSLog(@"%s - %f", __PRETTY_FUNCTION__, self.volumeSlider.value);
}

and also other way around, you can set volume by:

self.volumeSlider.value = < some value between 0.0f and 1.0f >;

Hope this helps (and that Apple doesn't remove MPVolumeSlider from MPVolumeView).

msrdjan
  • 815
  • 9
  • 8
1

try this

[[MPMusicPlayerController applicationMusicPlayer] setVolume:(use a value between 0.0 and 1.0)]

try this if you want to provide volume control

 MPVolumeView *myVolumeView =
    [[MPVolumeView alloc] initWithFrame: CGRectMake(70, 300, 200, 40)];
    [myVolumeView setShowsVolumeSlider:YES];
    [myVolumeView setShowsRouteButton:YES];
    [myVolumeView sizeToFit];
    [myVolumeView setBackgroundColor:[UIColor cyanColor]];
    CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI * 0.5);
    myVolumeView.transform = trans;
    [moviePlayerContlr.view addSubview: myVolumeView];

ALTERNATE:

UISlider  * aSlider;
    aSlider = [[UISlider alloc] initWithFrame:CGRectMake(70, 200, 100, 40)];
    [aSlider setMaximumValue:0];
    [aSlider setMaximumValue:1];
    [aSlider addTarget:self action:@selector(aSliderAction:) forControlEvents:UIControlEventValueChanged];
    [moviePlayerContlr.view addSubview:aSlider];

    UIImage *stetchTrack = [[UIImage imageNamed:@"faderTrack.png"]
                            stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
    [aSlider setThumbImage: [UIImage imageNamed:@"faderKey.png"] forState:UIControlStateNormal];
    [aSlider setMinimumTrackImage:stetchTrack forState:UIControlStateNormal];
    [aSlider setMaximumTrackImage:stetchTrack forState:UIControlStateNormal];
    CGAffineTransform tran = CGAffineTransformMakeRotation(M_PI * -0.5);
    aSlider.transform = trans;



- (void)aSliderAction:(id)sender
{
    NSLog(@"%d",(int)[aSlider value]);;

[[MPMusicPlayerController applicationMusicPlayer] setVolume:aSlider.value];
}
Muruganandham K
  • 5,271
  • 5
  • 34
  • 62
  • This is kind of what I've been trying. I'm trying to set it programatically based off of the user inputs not with a slider. – user3367709 Mar 01 '14 at 09:51
  • i stated here that these are the ways to set or control the volume. Look at the last method, instead of `aSlider.value` use user input. UISlider is a User input control... I think so... – Muruganandham K Mar 01 '14 at 09:55
  • MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer]; [musicPlayer setVolume:currentVol]; if I print out printf("volume:%f", musicPlayer.volume); I'm getting really weird numbers like 0 or 1 billion – user3367709 Mar 01 '14 at 10:03
  • From the apple documents: Use an MPMusicPlayerController object, or music player, to play media items from the device iPod library. There are two types of music player: The application music player plays music locally within your app. It is not aware of the iPod app’s now-playing item, nor does it affect the iPod state. – Muruganandham K Mar 01 '14 at 11:00
  • The iPod music player employs the built-in iPod app on your behalf. On instantiation, it takes on the current iPod app state, such as the identification of the now-playing item. If a user switches away from your app while music is playing, that music continues to play. The iPod app then has your music player’s most recently-set repeat mode, shuffle mode, playback state, and now-playing item. – Muruganandham K Mar 01 '14 at 11:01
  • so use `([MPMusicPlayerController applicationMusicPlayer])` instead of `([MPMusicPlayerController iPodMusicPlayer])` and try again. – Muruganandham K Mar 01 '14 at 11:05
1

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

#import <MediaPlayer/MediaPlayer.h>
// ...
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
musicPlayer.volume = slider.value; //float value for volume

Or Try with:

MPMusicPlayerController *musicPlayer = = [MPMusicPlayerController iPodMusicPlayer];
musicPlayer.volume = slider.value; //float value for volume
A J
  • 605
  • 4
  • 16
  • Accept the answer if it working. So other can take benefit from that. – A J Mar 01 '14 at 09:40
  • This is what I've been trying forever and I thought it was working but when I debut and print the values out to the console I either get 0.00000 or a really big number. – user3367709 Mar 01 '14 at 09:52
  • Are you talking about the values for slider? You can round the values of slider as : http://stackoverflow.com/a/2822749/905514 – A J Mar 01 '14 at 09:58
  • No my slider value is between 0 and 1 when I print out slider.value, but when I printout printf("volume:%f", musicPlayer.volume); i get really weird numbers – user3367709 Mar 01 '14 at 10:01
  • Not getting your exact issues. May be MPVolumeView class can help you. – A J Mar 01 '14 at 10:11
  • May be this can help: http://ios-blog.co.uk/tutorials/controlling-system-output-volume-with-the-mpvolumeview-class-part-one/ – A J Mar 01 '14 at 10:14
  • Sorry if it's not clear I'm kinda new at this. I have a double that I'm getting from a UISlider. The double is between 0 and 1 so that's correct. But when I set it [musicPlayer setVolume:double] this right here should work but it's not. To debug I printed out in the console musicPlayer.volume and I get really big numbers or 0 which is weird because the double I set it to is between 0 and 1 – user3367709 Mar 01 '14 at 10:27
  • Can you try that with executing application in IOS 6 and Volume method is deprecated in IOS 7. – A J Mar 01 '14 at 10:41
  • I know it's deprecated but it should still work. If not there has to be some other way to do it – user3367709 Mar 01 '14 at 21:39