1
votes

I've tried to make an UISlider that controls background music volume, and whatever I set the value to should be the volume of the music. The trouble is, when I run the app in simulator, and move the slider, the volume stays that of the original volume, it doesn't update.

I am new to programming so I still need help with the basics.

Here is my viewcontroller.h:

@interface FifthViewController : UIViewController <AVAudioPlayerDelegate> {

IBOutlet UISlider *slider;
IBOutlet UIAlertView *alert;
AVAudioPlayer *theAudio;


}


-(IBAction)alertbutton;
-(IBAction)changeVolume;
@end;

and here is the viewcontroller.m:

- (void)viewDidLoad

{
NSString *path = [[NSBundle mainBundle] pathForResource:@"background music" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
theAudio.numberOfLoops = -1;      
theAudio.volume=slider.value;


[super viewDidLoad];
}

I have connected "slider" to a horizontal slider and the sound plays and loops. And if I set the value of the slider higher or lower in Xcode, when I run the app, the volume adjusts accordingly. But if in the app I change the value of the slider, the volume remains the same . Can anyone look at my code and tell me what I'm doing wrong and how to fix it?

2
can you post the code attached to the action of your sliderWarren Burton
Can you give more details? Some code maybe? How do you set the volume? What framework do you use to play the sound?Valentin Radu

2 Answers

0
votes

Assuming you have an instance of AVAudioPlayer setup in your controller.

Set the slider to run from 0.0 to 1.0 and connect it to an action.

-(void)volumeValueChange:(UISlider *)sender
{
    (AVAudioPlayer *)myaudioplayer.volume = sender.value;
}
0
votes

//.m implementation where audioPlayer is an an object of AVAudioPlayer

-(void)volumeValueChange:(UISlider *)sender
{
    audioPlayer.volume = sender.value;
}