I searched everywhere but I couldn’t find an answer for my problem.
I play multiple sounds on my app and I let the user to adjust the volume with a button. So if the user wants, he can play with .5 volume. So I have this code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Orchestra" ofType:@"mp3"];
theOrchestras = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
[theOrchestras play];
and to reduce the volume this:
- (IBAction) volumeReduced {
theOrchestras.volume = 0.5;
}
When the sounds is playing the volume is reduced, no problem. But when the sounds stops, and plays again, the volume always goes back to 1.
To fix this I tried to save the volume as an Integer, but for some reason the value is always 0, I couldn’t make it work:
.m
int64_t * volumeSaved;
.h
- (IBAction) volumeReduced {
volumeSaved = .5;
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"Orchestra" ofType:@"mp3"];
theOrchestras = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
theOrchestras.volume = volumeSaved;
[theOrchestras play];
But the audio now always plays with 0 volume, muted.
Is there any way to do this with a button?
Thanks!
You’re declaring
volumeSavedas a pointer to an integer instead of an integer, but then assigning to it like it’s an integer. That’s one oddity.But your real problem is that the
volumeproperty inAVAudioPlayeris afloat, not aint64_t. So change the type to: