Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Audio still plays when iPhone volume is turned all the way down to silent

The way I am currently playing background music and other sounds in my app is behaving very strangely:

  • Turn the background music off, and the other sounds played are MUCH louder.
  • With the background music on, the the audio is MUCH quieter.
  • You can even turn the music on mid-sound, and the sound gets quieter.

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.

like image 858
RanLearns Avatar asked Oct 31 '11 05:10

RanLearns


1 Answers

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.

like image 184
amunita Avatar answered Sep 17 '22 14:09

amunita