【问题标题】:How to play audio over speakers rather than the much weaker ear speakers?如何通过扬声器而不是更弱的耳机播放音频?
【发布时间】:2013-01-19 19:42:19
【问题描述】:

我正在学习核心音频。由于某种原因,处理图的声音只能通过较弱的“耳机扬声器”播放(当您将设备放在耳边时),而不是通过 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
    );
}

在使用音频单元播放声音时,您在核心音频中的哪个位置说“通过扬声器播放”?

【问题讨论】:

    标签: iphone ios ipad core-audio


    【解决方案1】:

    您可以使用setCategorywithOption:

    [mySession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&audioSessionError];
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。原来这与“播放和记录”类别有关。只需要重定向音频输出。

      UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
      
      AudioSessionSetProperty (
          kAudioSessionProperty_OverrideAudioRoute,
          sizeof (audioRouteOverride),
          &audioRouteOverride
      );
      

      来源:

      【讨论】:

      • 试过了但没用,user523234 解决方案对我有用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      相关资源
      最近更新 更多