我想在應用程序轉到後臺後設置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和沉默的一切?也許還有其他的機會來睡覺音樂嗎?
你的意思是移動'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