开发者

Couldn't hear AVAudioRecorder's voice without headphones

开发者 https://www.devze.com 2023-03-11 01:09 出处:网络
I am recording voice on iPhone using AVAudioRecorder and these are my recorder settings: NSMutableDictionary* rec开发者_运维技巧ordSetting = [[NSMutableDictionary alloc] init];

I am recording voice on iPhone using AVAudioRecorder and these are my recorder settings:

 NSMutableDictionary* rec开发者_运维技巧ordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey]; 
    [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];

The problem is that I can't hear the recorded voice without headphones. I want to be able to hear the voice without headphones also. How should I change my code?


You have to set the AVAudiosession category to AVAudioSessionCategoryPlayback before playing the sound


Another thing to consider is that if you do not set up your AVAudioSession for playback, then the silence ringer switch on the side of the phone will make your audio not play over the speaker. In order for you to be able to play sound with a silenced phone make sure you are using something like this before triggering your AVAudioPlayer to play the file:

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];


Maybe force-routing the audio output to the speaker will help?

- (void) forceRouteAudioToSpeaker
{
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute,
        sizeof(audioRouteOverride), &audioRouteOverride);
}

Don’t forget to include <AudioToolbox/AudioServices.h>. On second thought, switching the audio category to plain playback should also do the trick, and you say it didn’t…

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号