2013-01-19 35 views
10

Tôi đang học âm thanh cốt lõi. Đối với một số lý do, âm thanh của biểu đồ xử lý chỉ phát qua "loa tai" yếu (khi bạn cầm thiết bị gần tai) nhưng không qua loa thông thường của iPhone.Làm thế nào để phát âm thanh qua loa thay vì loa tai yếu hơn nhiều?

Đây là mã mà thiết lập phiên âm thanh nhưng tôi không thể nhìn thấy nơi nó cấu hình con đường âm thanh:

- (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 
    ); 
} 

Tại thời điểm đó trong âm thanh lõi để bạn nói "chơi qua loa" khi chơi âm thanh với đơn vị âm thanh?

Trả lời

4

Tôi đã gặp vấn đề tương tự. Hóa ra nó là một cái gì đó để làm với thể loại "chơi và ghi". Chỉ cần chuyển hướng đầu ra âm thanh.

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 

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

Nguồn:

+0

Cố gắng này, nhưng nó đã không làm việc, user523234 giải pháp là những gì làm việc cho tôi. – blackmoon

6

Bạn có thể sử dụng setCategorywithOption:

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

Cảm ơn bạn, nó hoạt động hoàn hảo. – blackmoon

Các vấn đề liên quan