在我的应用程序进入后台后,我想设置 SoloAmbient(并使 iPhone 中的所有音乐静音)。
我写了这样的代码,但它不起作用
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 并使一切静音?也许还有其他机会睡音乐吗?