I have a background song that I use on an infinite loop while my app is in use. Except that the sound/volume is too high... I'd like it to be played at half the volume... This is how I load the music:
//background music for in-game loop
backgroundLoop = [[NSBundle mainBundle] pathForResource:@"Background" ofType:@"wav"];
GameMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:backgroundLoop] error:NULL];
GameMusic.delegate = self; //need <...> in .h
GameMusic.numberOfLoops = -1; //play infinite loop
I've tried a few things, but nothing affects the volume of the track. I've tried this:
GameMusic.volume = 0.5f;
and:
[GameMusic setVolume: 0.5f];
but it doesn't change the volume and still plays at the same max volume...
The strange thing is, if I set the volume to 0.0 ([GameMusic setVolume: 0.5f];
) it doesn't play at all because I'm guessing it's setting volume to zero... but why does 0.1f
or 0.5f
not adjust the volume when it's a linear scale from 0-1?
How can I lower the volume of the sound? It's far too loud.
Also, I tried editing the actual sounds in Garageband, but they won't export any sounds under a second long so it's not feasible to adjust them manually.
Thanks.
Update
A Wiki, shows the following file formats are supported:
- AAC (MPEG-4 Advanced Audio Coding)
- ALAC (Apple Lossless)
- AMR (Adaptive Multi-rate)
- HE-AAC (MPEG-4 High Efficiency AAC)
- iLBC (internet Low Bit Rate Codec)
- Linear PCM (uncompressed, linear pulse code modulation)
- MP3 (MPEG-1 audio layer 3)
- µ-law and a-law
Unfortunately, I changed my .wav
to a .mp3
and it still isn't working :(
I suspect that it is working but, because the sound is so loud to start with, you are not perceiving the difference (because of the logarithmic nature of the decibel scale of intensities). Try a much smaller number, such as 0.05
, perhaps.
It's finally working. The volume is adjusted by using lower linear values such as 0.01f
and 0.08f
.
I changed my file type to mp3
as wav
isn't supported:
backgroundLoop = [[NSBundle mainBundle] pathForResource:@"Background" ofType:@"mp3"];
and then set the volume of the loaded audio:
GameMusic.volume = 0.01f
and 0.01f
cuts the volume in half (approximately by my ears), but may be different depending on what track is used, if it's normalised or what file format it's encoded in.
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With