如何在扬声器上播放音频而不是耳机更弱?

ope*_*rog 10 iphone core-audio ipad ios

我正在学习核心音频.由于某种原因,处理图表的声音只能通过弱"耳机"(当您将设备放在耳边时)播放,而不是通过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
    );
}
Run Code Online (Sandbox Code Playgroud)

在使用音频单元播放声音时,你会说核心音频中的"播放扬声器"吗?

use*_*234 7

你可以使用 setCategory withOption:

[mySession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&audioSessionError];
Run Code Online (Sandbox Code Playgroud)


Neb*_*Fox 4

我有同样的问题。事实证明,这与“播放和录制”类别有关。只需要重定向音频输出即可。

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;

AudioSessionSetProperty (
    kAudioSessionProperty_OverrideAudioRoute,
    sizeof (audioRouteOverride),
    &audioRouteOverride
);
Run Code Online (Sandbox Code Playgroud)

来源: