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

2

10

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.

Coelostat answered 31/10, 2011 at 5:36 Comment(5)
Have you tested this on more devices? Is it consistent across Devices?Layette
It does NOT happen on the iPad (1st generation, iOS 5). That's the only other device I've been able to test on so far. The iPhone this is happening on is also iOS 5.Coelostat
Well that get's you an upvote on the question. Sure felt like it was a problem with your deviceLayette
What is your AVAudioSessionCategory set to?Hoodlum
Without seeing your code, I don't know, but try reading: #1659667Demona
C
1

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.

Crowther answered 14/12, 2011 at 3:55 Comment(0)
P
0

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.

Plagio answered 2/1, 2012 at 23:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.