ios中同时录音和播放

白云
2023-12-01

ios中要同时播放和录音,并扬声器播放,在登陆界面添加如下函数即可:

1、在viewDidLoad中调用一次(不用每次录音和播放时调用):

[selfSetRecordPlayAtSameTime_Func];


2、设置可以同时录音和播放、并扬声器播放函数:

// 需添加: AudioToolBox.framework
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#import <AudioToolbox/AudioToolbox.h>

// 设置录音和播放同时进行、且扬声器播放
-(void)SetRecordPlayAtSameTime_Func
{
    // 同时录音和播放
    UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
    
    // 扬声器播放
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
    
    AudioSessionSetActive(true);
    
    /*
    AVAudioSession* session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    [session setActive: YES error:nil];
     */
}








































 类似资料: