10

コアオーディオを学んでいます。何らかの理由で、処理グラフのサウンドは、iPhone の通常のスピーカーではなく、弱い「イヤー スピーカー」 (デバイスを耳に当てたとき) からのみ再生されます。

これはオーディオ セッションをセットアップするコードですが、オーディオ ルートを構成する場所がわかりません。

- (void) setupAudioSession {

    AVAudioSession *mySession = [AVAudioSession sharedInstance];

    // Specify that this object is the delegate of the audio session, so that
    //    this object's endInterruption method will be invoked when needed.
    [mySession setDelegate: self];

    // Assign the Playback category to the audio session.
    NSError *audioSessionError = nil;
    [mySession setCategory: AVAudioSessionCategoryPlayAndRecord//AVAudioSessionCategoryPlayback
                     error: &audioSessionError];

    if (audioSessionError != nil) {

        NSLog (@"Error setting audio session category.");
        return;
    }

    // Request the desired hardware sample rate.
    self.graphSampleRate = 44100.0;    // Hertz

    [mySession setPreferredHardwareSampleRate: graphSampleRate
                                        error: &audioSessionError];

    if (audioSessionError != nil) {

        NSLog (@"Error setting preferred hardware sample rate.");
        return;
    }

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

    if (audioSessionError != nil) {

        NSLog (@"Error activating audio session during initial setup.");
        return;
    }

    // Obtain the actual hardware sample rate and store it for later use in the audio processing graph.
    self.graphSampleRate = [mySession currentHardwareSampleRate];

    // Register the audio route change listener callback function with the audio session.
    AudioSessionAddPropertyListener (
        kAudioSessionProperty_AudioRouteChange,
        audioRouteChangeListenerCallback,
        self
    );
}

オーディオ ユニットでサウンドを再生するとき、コア オーディオのどの時点で「スピーカーで再生」と言いますか?

4

2 に答える 2

4

私も同じ問題を抱えていました。それは「再生と録音」のカテゴリに関係があることがわかりました。オーディオ出力をリダイレクトするだけです。

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;

AudioSessionSetProperty (
    kAudioSessionProperty_OverrideAudioRoute,
    sizeof (audioRouteOverride),
    &audioRouteOverride
);

ソース:

于 2013-02-12T15:24:28.920 に答える