Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lower volume of AVAudioPlayer sound

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 :(

like image 246
Reanimation Avatar asked May 06 '15 17:05

Reanimation


2 Answers

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.

like image 97
matt Avatar answered Oct 19 '22 19:10

matt


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.

like image 20
Reanimation Avatar answered Oct 19 '22 21:10

Reanimation