The way I am currently playing background music and other sounds in my app is behaving very strangely:
The strangest part: When the iPhone's volume is turned all the way down (muted) there should be no sounds at all. With the background music on but no device volume, it does what you would expect - you can't hear music or sound effects. But if the background music is turned off, the sound effects are still played and quite loudly even though the device itself is turned all the way down!
Here is my code...
For my background music:
AVAudioPlayer *musicPlayer;
- (void)playMusic {
if (musicPlayer == nil) {
NSURL *musicPath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"SongFile" ofType:@"mp3"]];
musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicPath error:nil];
musicPlayer.volume = 0.2f;
musicPlayer.numberOfLoops = -1;
}
[musicPlayer play];
}
- (void)stopMusic {
[musicPlayer stop];
}
For sounds during play:
#import "SoundEffect.h"
SoundEffect *sounds;
- (void)playSoundWithInfo:(NSString *)sound; {
NSString *path = [[NSBundle mainBundle] pathForResource:sound ofType:@"caf"];
sounds = nil;
sounds = [[SoundEffect alloc] initWithContentsOfFile:path];
[sounds play];
}
Any ideas would be greatly appreciated.
Maybe it is because you are using AVAudioPlayer for your music and SystemSounds to reproduce your sound effects. The volume set for AVAudioPlayer is in the app, but the volume set for SystemSounds is the systemVolume.
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