Lower audio level AVAudioPlayer

I have a background song that I use in an endless loop while my app is in use. Except the audio / volume is too high ... I would like it to play at half 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 keeps playing with the same maximum volume ...

Strange thing: if I set the volume to 0.0 ( [GameMusic setVolume: 0.5f];

) it doesn't play at all, because I assume it sets the volume to zero ... but why 0.1f

or 0.5f

not adjust the volume when the linear scale is from 0-1?

How do I decrease the sound volume? It's too loud.

Also, I tried to edit the actual sounds in Garageband, but they won't export any sounds for a second, so it isn't possible to manually adjust them.

Thank.

Update

Wiki , the following file formats are shown:

- 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 mine .wav

to .mp3

and it still doesn't work :(

+3


source to share


3 answers


I suspect it works, but since the sound is so loud to start with, you cannot perceive the difference (due to the logarithmic nature of the decibel scale). Try a much smaller number, for example 0.05

.



+8


source


Finally it works. The volume is adjusted using lower linear values ​​such as 0.01f

and 0.08f

.

I changed my file type to mp3

as it is wav

not supported:

    backgroundLoop = [[NSBundle mainBundle] pathForResource:@"Background" ofType:@"mp3"];

      

and then set the volume of the downloaded audio:



    GameMusic.volume = 0.01f

      

and 0.01f

cuts the volume in half (roughly on the ears), but may differ depending on which track is used, if it's normalized, or what file format it's encoded in.

Hope this helps.

+2


source


I was getting errors in Swift 3.0 using this syntax. I resolved this through trial and error. Follow these steps to configure volumes:

nameOfFile.volume = 1.0

Put "f". Anything above 1.0 will increase the volume, below it will decrease. Hope this helps. Greetings.

0


source







All Articles