2

何時間も前から AVAudioSession の問題を解決しようとしていますが、成功しませんでした!!

多くの人が endInterruption の問題について話しているのを見つけましたが、誰もこのエラーについて話していません:

Unable to reactivate the audio session after the interruption ended: Error Domain=NSOSStatusErrorDomain Code=560161140 "The operation couldn’t be completed. (OSStatus error 560161140.)"

このコードを ASCII に変換してオーディオ コードの結果を確認しようとしましたが、この番号に関連するものは何もありません。

セッションを開始するための私のコード:

- (void) setupAudioSession {

mySession = [AVAudioSession sharedInstance];
[mySession setDelegate: self];

NSError *audioSessionError = nil;
[mySession setCategory: AVAudioSessionCategoryPlayback error: &audioSessionError];

if (audioSessionError != nil) {
    NSLog (@"Error setting audio session category: %@", [audioSessionError description]);
    return;
}

// Activate the audio session
[mySession setActive: YES error: &audioSessionError];

if (audioSessionError != nil) {
    NSLog (@"Error activating audio session during initial setup: %@", [audioSessionError description]);
    return;
}

// Prepare audio file to play
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *bgSoundURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"bg_sound" ofType:@"aif"]];

bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgSoundURL error:&audioSessionError];
if (!bgPlayer) {
    NSLog(@"No bgPlayer: %@", [audioSessionError description]);  
}

[bgPlayer setNumberOfLoops:-1];
[bgPlayer prepareToPlay];

}

endInterruption メソッドのコード:

- (void) endInterruptionWithFlags: (NSUInteger) flags {

if (flags & AVAudioSessionInterruptionFlags_ShouldResume) {

    NSError *endInterruptionError = nil;
    [[AVAudioSession sharedInstance] setActive: YES error: &endInterruptionError];
    if (endInterruptionError != nil) {

        NSLog (@"Unable to reactivate the audio session after the interruption ended: %@", [endInterruptionError description]);
        return;

    } else {

        NSLog (@"Audio session reactivated after interruption.");

        if (interruptedWhilePlaying) {

            self.interruptedWhilePlaying = NO;

            // Resume playback by sending a notification to the controller object, which
            //    in turn invokes the playOrStop: toggle method.
            NSString *MixerHostAudioObjectPlaybackStateDidChangeNotification = @"MixerHostAudioObjectPlaybackStateDidChangeNotification";
            [[NSNotificationCenter defaultCenter] postNotificationName: MixerHostAudioObjectPlaybackStateDidChangeNotification object: self]; 

        }
    }
}

}

このコードの大部分は、Apple Mixer Host Sample から抽出されたものです。そして、サンプルが正常に動作するのは奇妙です!

何が悪いのか理解できますか?

ありがとう。

4

4 に答える 4

2

「このデリゲート メソッドが flags パラメータで AVAudioSessionInterruptionFlags_ShouldResume 定数を受け取った場合、オーディオ セッションはすぐに使用できる状態になります。」

コールバックを正しく処理しませんでした。AVAudioSessionInterruptionFlags_ShouldResume を受け取ったら、オーディオ セッションはすでに使用可能です。DIFFERENT フラグを取得したら、setActive を呼び出す必要があります。

それが役に立てば幸い...

于 2011-08-09T16:03:49.647 に答える
2

オーディオ コードの設計方法を変更して問題を解決しました。AVAudioSession とそのデリゲート メソッドを使用する代わりに、C スタイルに変更してオーディオを操作します。

私は機能を実装しました:

void interruptionListenerCallback (void *inUserData, UInt32 interruptionState) {}

そして、それは次のように初期化されました:

AudioSessionInitialize (NULL, NULL, interruptionListenerCallback, self);

私の -(id) init メソッド内。

それがあなたにも役立つことを願っています。一番。

于 2011-02-24T15:44:13.323 に答える
1

@Trinca を使用して、「ARC'ed」プロジェクトで行ったことに答えます。

AudioSessionInitialize (NULL, NULL, interruptionListenerCallback, (__bridge void *)(self.player));

self.player は、コールバック関数に渡すプレーヤー インスタンスです。

次に、コールバックを実装します。

void interruptionListenerCallback (void *inUserData, UInt32 interruptionState) {
   NSLog(@"itnerp state %li",interruptionState);
   NSLog(@"inUserData %@",inUserData);
   AVAudioPlayer *pl = (__bridge AVAudioPlayer*)inUserData;
   switch (interruptionState) {
    case 0:
        [pl play];
    break;

    case 1:
        [pl pause];
    break;
   }

}

幸運を

シャニ

于 2012-06-22T14:40:47.383 に答える