I've noticed that when my app start, the music I'm listening is automatically stopped, and I've noticed that when I start some other apps, the music just continue... this means that I don't know how to manage the actual playing music in the device to let it plays or stop.
我注意到,当我的应用程序启动时,我正在收听的音乐会自动停止,我注意到当我启动其他应用程序时,音乐会继续...这意味着我不知道如何管理设备中的实际播放音乐,让它播放或停止。
I'm developing a game with obj-c and cocos2d btw, I've searched but sadly I've found nothing... so here's my question, how can I let the music I'm listening with my device continue to play even if I start the app ?
我正在用obj-c和cocos2d btw开发一款游戏,我搜索过但遗憾的是我什么都没找到......所以这是我的问题,我怎么能让我用我的设备听的音乐继续播放如果我启动应用程序?
edit: I'm using SimpleAudioEngine to start a background music and some sound effects in my app
编辑:我正在使用SimpleAudioEngine在我的应用程序中启动背景音乐和一些声音效果
3 个解决方案
#1
11
Place this line in your application:didFinishLaunchingWithOptions: method of your AppDelegate or in general before using the audio player.
将此行放在您的应用程序中:didFinishLaunchingWithOptions:AppDelegate的方法,或者在使用音频播放器之前。
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
According to the documentation, the AVAudioSessionCategoryAmbient category is
根据文档,AVAudioSessionCategoryAmbient类别是
for an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off. This category is also appropriate for “play along” style apps, such as a virtual piano that a user plays over iPod audio. When you use this category, audio from other apps mixes with your audio. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).
对于声音播放非主要的应用程序 - 也就是说,您的应用程序可以在声音关闭时成功使用。此类别也适用于“播放”风格的应用程序,例如用户在iPod音频上播放的虚拟钢琴。使用此类别时,来自其他应用程序的音频会与您的音频混合。屏幕锁定和静音开关(在iPhone上称为Ring / Silent开关)可以使您的音频静音。
please import AVFoundation/AVFoundation.h
请导入AVFoundation / AVFoundation.h
#2
2
Swift:
Before playing enter this line:
在玩之前输入这一行:
let audioSession = AVAudioSession.sharedInstance()
if audioSession.otherAudioPlaying {
_ = try? audioSession.setCategory(AVAudioSessionCategoryAmbient, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
}
#3
-4
When your app starts playing music, it will pause any audio currently playing. For obvious reasons. If you want your music to carry on playing when the app launches then don't let the app play any music.
当您的应用开始播放音乐时,它会暂停当前播放的所有音频。原因很明显。如果您希望在应用启动时继续播放音乐,请不要让应用播放任何音乐。
#1
11
Place this line in your application:didFinishLaunchingWithOptions: method of your AppDelegate or in general before using the audio player.
将此行放在您的应用程序中:didFinishLaunchingWithOptions:AppDelegate的方法,或者在使用音频播放器之前。
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
According to the documentation, the AVAudioSessionCategoryAmbient category is
根据文档,AVAudioSessionCategoryAmbient类别是
for an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off. This category is also appropriate for “play along” style apps, such as a virtual piano that a user plays over iPod audio. When you use this category, audio from other apps mixes with your audio. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).
对于声音播放非主要的应用程序 - 也就是说,您的应用程序可以在声音关闭时成功使用。此类别也适用于“播放”风格的应用程序,例如用户在iPod音频上播放的虚拟钢琴。使用此类别时,来自其他应用程序的音频会与您的音频混合。屏幕锁定和静音开关(在iPhone上称为Ring / Silent开关)可以使您的音频静音。
please import AVFoundation/AVFoundation.h
请导入AVFoundation / AVFoundation.h
#2
2
Swift:
Before playing enter this line:
在玩之前输入这一行:
let audioSession = AVAudioSession.sharedInstance()
if audioSession.otherAudioPlaying {
_ = try? audioSession.setCategory(AVAudioSessionCategoryAmbient, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
}
#3
-4
When your app starts playing music, it will pause any audio currently playing. For obvious reasons. If you want your music to carry on playing when the app launches then don't let the app play any music.
当您的应用开始播放音乐时,它会暂停当前播放的所有音频。原因很明显。如果您希望在应用启动时继续播放音乐,请不要让应用播放任何音乐。