iPhone仅为应用程序设置播放声音的音量

时间:2011-04-14 16:37:55

标签: iphone audio audiosession mpvolumeview

我目前想知道如何

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

我可以使用 AudioSession 进行a)和 MPVolumeView 进行b)。

现在我读到,我无法为AudioSessions设置音量,因为它们的音量基于系统音量,这在我的基础上是不好的。在进行音量控制播放时停止播放iPod音乐会更糟糕。

我注意到runmeter(不想广告,仅作为示例)例如有一个滑块,无论系统音量和/或ipods音量是多少,都会控制应用程序音量。

所以我几乎可以将手机静音,放在iPod中间并最大化应用程序量,工作正常。

有没有办法/暗示如何完成这项工作而不被苹果放弃?

有什么建议吗?

感谢。


编辑:感谢badgrr解决这个问题,我的最终解决方案是,通过使用CocosDenshin:

appDelegate,applicationDidEnterBackground:

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

appDelegate,applicationWillEnterForeground: - 只是为了确保!

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

播放时,我还需要知道声音文件何时完成,播放下一个声音文件,因为我不希望以混合的方式构建句子。话。比如说“你好”然后说“世界”:

初始化我的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>  
播放声音:

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

并在当前停止后播放下一个(我的audioQueue是一个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];
    }
}

希望它可以帮助任何遇到同样麻烦的人!

1 个答案:

答案 0 :(得分:1)

从评论转到答案,以便其他人阅读更容易。

看看CocosDenshion。它使用OpenAL语音,并可以通过不同界面控制的背景声音播放它们。如果需要,可以使用它而无需使用Cocos2d。

为了防止声音效果停止iPod库播放,您需要使用以下行指定自定义音频会话:

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