SoloAmbient和applicationDidEnterBackground
问题描述:
我想在应用程序转到后台后设置SoloAmbient(并在iPhone中静音所有音乐)。SoloAmbient和applicationDidEnterBackground
我写了这样的代码,但它不工作
MyAppDelegate.h
@interface MyAppDelegate : UIResponder <UIApplicationDelegate> {
AVAudioSession *audioSession;
}
@property (strong, nonatomic) AVAudioSession *audioSession;
MyAppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Application entered background state.");
//[self.DelcloseSession setCategory:AVAudioSessionCategorySoloAmbient error:nil];
//[self.DelcloseSession setActive:YES error:nil];
audioSession = [AVAudioSession sharedInstance];
[audioSession setDelegate:self];
NSError *averr = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&averr];
if(averr)
{
NSLog(@"audioSession: %@ %d %@", [averr domain], [averr code], [[averr userInfo] description]);
}
averr = nil;
[audioSession setActive:YES error:&averr];
if(averr)
{
NSLog(@"audioSession: %@ %d %@", [averr domain], [averr code], [[averr userInfo] description]);
}
averr = nil;
[audioSession setCategory:AVAudioSessionCategorySoloAmbient error:&averr];
if(averr)
{
NSLog(@"audioSession: %@ %d %@", [averr domain], [averr code], [[averr userInfo] description]);
}
实际上应用给我发送日志“audioSession:NSOSStatusErrorDomain 560161140(null)”和iPhone上的音乐(从iPod或其他应用程序)我还在玩。
如何解决?如何做,当应用程序去背景它打开SoloAmbient和沉默的一切?也许还有其他的机会来睡觉音乐吗?
答
您的应用程序需要在后台模式下请求音频背景plist,设置音频会话并开始播放声音(沉默等),然后再退出活动并进入后台。否则,音频会话属于新的前台应用程序。
答
您可以使用媒体播放器框架
#import <MediaPlayer/MediaPlayer.h>
,然后这个
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) {
//iPod is playing
} else {
//iPod is not playing
}
检测iPod播放状态,从那里去
+0
但我不想只停止iPod。我想要沉默每个可能的音乐,比如流媒体,来自其他应用的音乐。这就是为什么我想使用SoloAmbient而不是基本停止iPod。 – 2012-03-06 21:35:27
你的意思是移动'audioSession = [AVAudioSession sharedInstance] ; \t [audioSession setDelegate:self];'to didFinishLaunchingWithOptions? “开始播放声音”你是什么意思? – 2012-03-06 15:12:33
如果应用程序没有播放声音,它在进入后台时不会保持运行状态,这意味着它将不再对音频会话类型有任何发言权。 – hotpaw2 2012-03-06 17:17:56
我有一个循环播放'沉默'的音乐,以保持应用程序。我要么在那里使用会话:'[self.silentSession setCategory:AVAudioSessionCategoryPlayback error:nil]; UInt32 doSetProperty = 1; AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof(doSetProperty),&doSetProperty); [self.silentSession setActive:YES error:nil];'但它不在delegate中。 – 2012-03-06 17:52:26