Reputation: 2730
I am playing a sound in my app using the following code:
NSString *soundFilePath = [NSString stringWithFormat:@"%@/samsung_whistle.mp3", [[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = 0;
[player play];
This seems to work, however when I set the system volume (using the hardware buttons on the device) it doesn't have any effect on my sound. If I set the volume all the way down (so the device says "sounds off") I can still hear my sound playing. And if I set the volume all the way up my sound is not played any louder.
I know that I can set the volume of my sound with
player.volume = someFloatBetween0And1;
But this hardcodes the volume. How can I retrieve the system sound volume and set it for my sound, so users can properly adjust their volume?
Upvotes: 2
Views: 652
Reputation: 2646
The volume you set on your player and the system volume are independent.
It's like when you watch a movie in your favorite video player. It has a settable volume. Still, you have the system volume (in your system tray perhaps) that you can set independently from the video player volume.
If the system volume is at 50% and your player's volume is at 50% as well, the sound is 4 times weaker than if both are at 100%.
Conclusion: if your player is set at 100% and the system volume is 0% then... silence. Which is a good use case. Because if apps could access the system volume, when I get a phone call I would need to go to each app and get the darn sound level to 0 :D
Upvotes: 3