Rory
Rory

Reputation: 11

Volume control in iPhone OS from another view controller?

If I am playing a sound in one view, does anyone know if it is possible to control the volume from another, if it is can someone explain how? I can't figure it out, I have no code to show for the volume.

The sound is called from one view and the volume slider is on another. I have coded both.

The code for the sound is

 #import `<AVFoundation/AVAudioPlayer.h`>  
 #import "LeftViewController.h"


@implementation LeftViewController




- (IBAction)buttonrm:(id)sender
{
 [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)playl {

 [theAudio play];



}

- (IBAction)pausel {

 [theAudio pause];

}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

 NSString *path =[[NSBundle mainBundle] pathForResource:@"The Noisettes - Never Forget You" ofType:@"mp3"];
 theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
 theAudio.delegate = self;
 //[theAudio play];




    [super viewDidLoad];
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}



- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

the code for the slider is

 - (void)viewDidLoad {
    [super viewDidLoad];

 CGRect sliderRect = CGRectMake(46,124,169,0);
 UISlider *VolumeL = [[UISlider alloc] initWithFrame:sliderRect];
 VolumeL.minimumValue = 0;
 VolumeL.maximumValue = 100;
 VolumeL.continuous = YES;


 UIImage *sliderctrl = [UIImage imageNamed:@"VolumeL.png"];
 //UIImage *stetchLeftTrack = [[UIImage imageNamed:@"volumel12.png"]
 //stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];


 [VolumeL setThumbImage:sliderctrl forState:UIControlStateNormal];
 //[VolumeL setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];

 [VolumeL addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

 VolumeL.transform = CGAffineTransformRotate(VolumeL.transform, 270.0/180*M_PI);

 [self.view addSubview:VolumeL];

 [VolumeL release];


 }

Upvotes: 1

Views: 894

Answers (2)

Cameron Lowell Palmer
Cameron Lowell Palmer

Reputation: 22245

Use the delegate pattern

In the second view controller create a protocol called MyProtocol for example with one method:

- (void)didUpdateVolume:(NSUInteger)volume;

Also create a delegate instance variable to hold the delegate reference

@property (nonatomic, assign) id<MyProtocol> delegate;

And don't forget to synthesize it in the implementation.

When the volume value is updated you would send the value to the delegate

[self.delegate didUpdateVolume:newValue];

Back in the first controller adopt the MyProtocol protocol, implement didUpdateVolume, and set the value in your player.

Upvotes: 1

Related Questions