7

我注意到一些应用程序在启动时以编程方式静音 iTunes(如果它正在运行)。这是如何实现的?我有一个带有背景音乐的游戏,并且想停止 iTunes 或至少收到一条消息,指出 iTunes 正在播放,以便我可以停止游戏的背景音乐。

谢谢,马克。

4

2 回答 2

8

You don't need to. With Audio Session you can decide how the audio should behave.

From the Audio Session Programming Guide:

With the audio session interface, you specify aspects of your application’s audio behavior and configure it to live harmoniously within the iPhone audio environment. You start by asking yourself questions such as these:

  • Do you want your audio to be silenced by the Ring/Silent switch? The answer is probably “yes” if audio is not essential to using your application successfully. (Users will appreciate being able to run your game in a meeting with no one the wiser.)

  • Do you want iPod audio to continue playing when your audio starts? This could be appropriate for a virtual piano, letting users play along to songs in their libraries. You’d want iPod audio to stop, however, for a streaming radio application.

You probably want this:

UInt32 sessionCategory = kAudioSessionCategory_SoloAmbientSound;
AudioSessionSetProperty (
    kAudioSessionProperty_AudioCategory,
    sizeof (sessionCategory),
    &sessionCategory
);

For more behaviour types, check the Audio Session Categories, or read the entire Audio Session Programming Guide.

于 2009-05-29T15:11:24.927 回答
0

我遇到了相反的问题。我的应用在启动后播放一段没有声音的短视频。这导致在后台播放的 iTunes 音乐静音。

为了保持音乐播放,我在 applicationDidFinishLaunching 中添加了这个:

NSError* error;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &error];    
    if (error) NSLog(@"Unable to configure Audio");
于 2012-07-02T22:47:56.560 回答