Is MPVolumeView supposed to change volume in simulator?

Discussion in 'iOS Programming' started by Danneman101, Mar 5, 2009.

  1. Danneman101 macrumors 6502

    Aug 14, 2008
    Ive implemented a MPVolumeView with this code:

    MPVolumeView *myVolume = [[MPVolumeView alloc] initWithFrame:CGRectMake(10, 435, 300, 0)];
    [myVolume sizeToFit];
    [moviePlayerWindow addSubview:myVolume];
    [myVolume release];			
    However, when running in the simulator it is set to the far left, which would indicate that the sound is turned off. But it's not. And when moving the bar to the right, it doesnt change the sound-level at all.

    So, is this just with the simulator, since the MPVolumeView changes the system volume? Will the MPVolumeView change the volume when run on a real iPhone?
  2. Pring macrumors 6502

    Sep 17, 2003
    Correct, it'll work in the device but not the simulator.
  3. Danneman101 thread starter macrumors 6502

    Aug 14, 2008
  4. TenTouch macrumors newbie

    Nov 17, 2009
    100% tested

    You need to add AVFoundation.framework to the project


    #import <UIKit/UIKit.h>
    #import <MediaPlayer/MediaPlayer.h>
    #import <AVFoundation/AVAudioPlayer.h>
    @interface VolSliderViewController : UIViewController {
    UIView *volumeViewSlider;
    AVAudioPlayer *audioPlayer;


    - (void)viewDidLoad {
    [super viewDidLoad];
    //play audio 
    //the system volume can only be changed if audio is playing otherwise the ringer volume is changed
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/ramzes2.mp3", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -1;
    if (audioPlayer == nil)
    NSLog([error description]);
    [audioPlayer play];
    //implement a system volume slider 
    MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame: CGRectMake(50, 100, 200, 200)] autorelease];
    [volumeView sizeToFit];
    [self.view addSubview:volumeView];
    for (UIView *view in [volumeView subviews]) {
    if ([[[view class] description] isEqualToString:@"MPVolumeSlider"]) {
    volumeViewSlider = view;
    There is no need to use "_updateVolumeFromAVSystemController" it works without it.

Share This Page