简体   繁体   English

iPhone:如何检测iTunes是否正在播放?

[英]iPhone: How to detect if iTunes is playing?

I noticed that some apps programmatically mute itunes (if its running) at launching. 我注意到一些应用程序在启动时以编程方式静音itunes(如果它正在运行)。 How is this achieved? 这是如何实现的? I have a game with background music and would like to either stop itunes or get at least a message that itunes is playing so that I can stop the game's background music. 我有一个带背景音乐的游戏,想要停止itunes或至少得到itunes正在播放的消息,以便我可以停止游戏的背景音乐。

thx, marc. thx,marc。

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. 通过音频会话界面,您可以指定应用程序音频行为的各个方面,并将其配置为在iPhone音频环境中和谐地生活。 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? 您希望在音频启动时继续播放iPod音频吗? 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. 但是,对于流媒体广播应用,您需要停止iPod音频。

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 . 有关更多行为类型,请查看“ 音频会话类别” ,或阅读整个“ 音频会话编程指南”

I had the opposite problem. 我遇到了相反的问题。 My app plays a short video with no sound after launch. 我的应用程序在播放后播放一段没有声音的短视频。 This caused the iTunes music playing in the background to mute. 这导致iTunes音乐在后台播放静音。

In order to keep the music playing, I add this in the applicationDidFinishLaunching: 为了保持音乐播放,我在applicationDidFinishLaunching中添加了这个:

NSError* error;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &error];    
    if (error) NSLog(@"Unable to configure Audio");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM