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

标签 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

我可以通过对 a) 使用 AudioSession 和对 b) 使用 MPVolumeView 来分开。

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

我注意到 runmeter(不想打广告,仅作为示例)例如有一个 slider ,它控制应用程序的音量,而不管系统音量和/或 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];
    }
}

希望它能帮助任何和我有同样问题的人!

最佳答案

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

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

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

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

关于iPhone设置音量以仅播放应用程序的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5666542/

相关文章:

actionscript-3 - AS3 SoundChannel 函数中音频的位置

javascript - HTML5 音频不会在 Firefox 中播放第二次

iphone - RemoteIO 的 AudioSession 设置与 iPod 一起玩

ios - 未调用 Audio Session 中断监听器

javascript - jQuery fancybox click function for dynamically added content not firing on iOS, iPhone, or iPad

iphone - 从应用程序访问 iPhone 联系人

iphone - 即使在重新启动我的开发 Mac 之后,实体仍然不符合键的键值编码

ios - UITableView 播放音频文件不正确

iphone - 将 CLLocationCoordinate2D 存储到 NSUserDefaults

ios - 录音时播放音乐,无需重新路由