0

アプリケーションがバックグラウンドに移行した後、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 を開いてすべてを無音にする方法を教えてください。たぶん、音楽を眠らせる機会は他にありますか?

4

2 に答える 2

0

アプリは、バックグラウンドモードのplistでオーディオのバックグラウンドを要求し、オーディオセッションを設定し、サウンドの再生を開始する必要があります(無音など)。それ以外の場合、オーディオセッションは新しいフォアグラウンドアプリに属します。

于 2012-03-06T15:04:18.720 に答える
0

mediaplayer フレームワークを使用して iPod の再生状態を検出できます

#import <MediaPlayer/MediaPlayer.h>

そして、これ

if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) {
    //iPod is playing
} else {
    //iPod is not playing
}

そしてそこから行く

于 2012-03-06T19:26:49.977 に答える