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

6.4k Views Asked by At

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.

2

There are 2 best solutions below

0
On

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.

0
On

You need to set your AVAudioSession before playback of sounds using the AVAudioPlayer and MPMediaPlayer in your app, and there are different categories for it based on how you want the audio to interact. While I have no idea why the music would still play even on zero volume, the other problems seem to be from an AVAudioSessionCategory setting you don't want. In the initialization of your app or wherever you begin to play sounds, try the following code:

[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

This code initializes the AVAudioSession and sets it to a category where the sounds won't mix with other background sounds (they'll be stopped once you activate the session) and where your app has priority. Note that screen locking and the silence switch will still allow sounds to play (at their full volume). You can set that specifically, I believe, but I don't know the exact code.

If you want different behavior for audio, check out the other categories you could set it to in Apple's documentation of the AVAudioSession Class. Other options are:

AVAudioSessionCategoryAmbient;
AVAudioSessionCategorySoloAmbient;
AVAudioSessionCategoryPlayback;
AVAudioSessionCategoryRecord;
AVAudioSessionCategoryPlayAndRecord;
AVAudioSessionCategoryAudioProcessing;

Anyway, hopefully the audio session issue was causing those problems. Let me know if that worked.