[英]- (void)applicationDidEnterBackground:(UIApplication *)application
[英]SoloAmbient and applicationDidEnterBackground
我的應用程序轉到背景后,我想設置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(空)”,並且iPhone(通過iPod或其他應用程序)上的音樂仍在播放。
如何解決? 怎么做,當應用程序轉到后台時,它會打開SoloAmbient並使所有內容靜音? 也許還有其他機會可以聽音樂了嗎?
您的應用將需要在其背景模式plist中請求音頻背景,設置音頻會話並開始播放聲音(靜音等),然后退出活動狀態並進入背景。 否則,音頻會話屬於新的前台應用程序。
您可以使用mediaplayer框架檢測iPod的播放狀態
#import <MediaPlayer/MediaPlayer.h>
然后這個
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) {
//iPod is playing
} else {
//iPod is not playing
}
然后從那里去
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.