【问题标题】:iPhone setting sound volume for playing sounds for app onlyiPhone设置音量以仅播放应用程序的声音
【发布时间】:2011-04-14 16:37:55
【问题描述】:

我现在想知道如何

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];
    }
}

希望它能帮助遇到和我一样烦恼的人!

【问题讨论】:

  • 您可以使用 OpenAL 来制作您的声音吗?这将允许您单独控制音量。
  • badgerr,感谢您的提示!我试着查了一下,没有清楚地注意到我是否也可以在后台播放 openAL?我将此信息添加到我的问题中,我只是忘了命名这个主要部分..
  • 看看CocosDenshion。它使用 OpenAL 处理声音,并且可以通过不同界面控制的背景声音播放它们
  • 好的,我会再试一次,也许我要快点!谢谢獾!非常感谢!

标签: iphone audio audiosession mpvolumeview


【解决方案1】:

从 cmets 转为回答,以便其他人更容易阅读。

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

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

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

【讨论】:

  • badgerr,好像你有一些经验。用它。现在因为我的时间不多了(假期旅行,4 小时后的航班),我需要完成这件事,实际上我只是无法在 CDSoundEngine 中弄清楚在哪里告诉不要暂停 ipod。它通过使用 alloc/init、loadBuffer w/ filepath 和 playSound 来实现。没有找到文档提示。有什么线索吗?
  • 是的,我在iPhone游戏中使用过,同时播放音效和背景音乐。
  • 来自同一个应用程序的背景音乐,我猜不是 ipod?
  • CocosDenshion + iPod 需要自定义音频会话。将其放在应用程序开头附近的某个位置: [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:NULL];
  • 很好,但现在因为它的环境,它非常安静!任何线索如何摆脱这种沉默?例如,对于 systemSoundsession,有一个混合属性。原因是,用户应该能够根据自己的意愿设置音量。不是苹果;)
猜你喜欢
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 2015-11-01
  • 2013-01-18
  • 1970-01-01
相关资源
最近更新 更多