iphoneaudioaudiosessionmpvolumeview

iPhone setting sound volume for playing sounds for app only


i currently wonder how to

a) Play a sound next to iPod/Other music sources
b) beeing able to control the volume of that played sound (only)
c) **important** both combined!!
d) all of it also on background/mt

I am able to do both sepperate by using AudioSession for a) and MPVolumeView for b).

now i read, that i cannot set volume for AudioSessions, since their volume is based on systems volume, which is bad in my base. Stopping the iPod music while having volume-controled playback is even worse.

i noticed that runmeter (dont want to ad, just as an example) for example has a slider, that controls the apps volume regardless of what the systems volume and/or ipods volume is.

so i can nearly mute the phone, middle the ipod and maximize the apps-volume, working fine.

is there any way/hint how to get this done w/o getting dropped by apple?

any suggestions?

Thanks.


EDIT: thanks to badgrr for solving this, my final solution was, via using CocosDenshin:

appDelegate, applicationDidEnterBackground:

[[CDAudioManager sharedManager] setMode:kAMM_MediaPlayback];
[[CDAudioManager sharedManager] setResignBehavior:kAMRBDoNothing autoHandle:NO];

appDelegate, applicationWillEnterForeground: - just to get sure!

[[CDAudioManager sharedManager] setResignBehavior:kAMRBDoNothing autoHandle:NO];

playback, i also needed to know when a soundfile was done, to play the next, since i do not want to have built sentences in a mix of simult. words. like saying "Hello" then "world":

on initializing my Soundplayer:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&err];
[[CDAudioManager sharedManager] setMode:kAMM_MediaPlayback];
soundEngine = [[CDAudioManager sharedManager] audioSourceForChannel:kASC_Right];
soundEngine.delegate = self; // ensure to implement <CDLongAudioSourceDelegate>  

playing sounds:

- (void)playSound:(NSString *)soundFile
{   
 [soundEngine load:[NSString stringWithFormat:@"%@.caf",soundFile]];
 [soundEngine play];
 ....
}

and playing the next one, after current stopps (my audioQueue is an NSMutableDictionay)

- (void) cdAudioSourceDidFinishPlaying:(CDLongAudioSource *) audioSource
{
    if ([self.audioQueue count] > 0) 
    {
        [self.audioQueue removeObjectAtIndex:0];
    }

    if ( [self.audioQueue count] > 0 ) 
    {
        NSString *file = [self.audioQueue objectAtIndex:0];
        [self playSound:file];
    } 
    else 
    {
        //NSLog(@"SoundsManager::cdAudio.. Done, queue empty");
            [self resumeIPod];
    }
}

hope it helps out anybody having same trouble as i do!


Solution

  • Moving from comments to answer so it's a bit easier for others to read.

    Have a look at CocosDenshion. It uses OpenAL for sounds, and can play them over background sounds controlled with a different interface. It is possible to use it without having to use Cocos2d, if required.

    In order to prevent the sound effects stopping iPod library playback, you need to specify a custom audio session with the following line:

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:NULL];