文档

自定义音频采集

ARTC SDK提供了灵活的自定义音频采集功能,此功能不仅支持用户自行管理音频设备,还允许在阿里云管理音频采集机制与用户管理音频采集机制之间的动态切换。

实现原理

image

代码实现

打开或关闭内部采集

Android平台

/* 动态关闭阿里内部采集 */
String parameter = "{\"audio\":\"{\\\"enable_system_audio_device_record\\\":\\\"FALSE\\\"}\"}";
mAliRtcEngine.setParameter(parameter);

/* 动态打开阿里内部采集 */
String parameter = "{\"audio\":\"{\\\"enable_system_audio_device_record\\\":\\\"TRUE\\\"}\"}";
mAliRtcEngine.setParameter(parameter);

iOS平台

/* 动态关闭阿里内部采集 */
[_engine setParameter:@"{\"audio\":\"{\\\"enable_system_audio_device_record\\\":\\\"FALSE\\\"}\"}"];

/* 动态打开阿里内部采集 */
[_engine setParameter:@"{\"audio\":\"{\\\"enable_system_audio_device_record\\\":\\\"TRUE\\\"}\"}"];

Windows平台

/* Windows支持创建时指定开启/关闭音频采集 */
/* 关闭阿里内部采集 */
char* extra = "{\"user_specified_enable_use_virtual_audio_device\":\"TRUE\", \"user_specified_use_external_audio_record\":\"TRUE\"}";
mAliRtcEngine = AliRtcEngine.Create(extra);

/* 开启阿里内部采集 */
char* extra = "{\"user_specified_enable_use_virtual_audio_device\":\"FALSE\", \"user_specified_use_external_audio_record\":\"FALSE\"}";
mAliRtcEngine = AliRtcEngine.Create(extra);

添加外部音频流

Android平台

/* 根据自己的业务设置对应的参数 */
AliRtcEngine.AliRtcExternalAudioStreamConfig config = new AliRtcEngine.AliRtcExternalAudioStreamConfig();
config.playoutVolume = currentAudioPlayoutVolume;
config.publishVolume = currentAudioPublishVolume;
config.channels = 1;
config.sampleRate = 48000;
audioStreamID = mAliRtcEngine.addExternalAudioStream(config);

iOS平台

/* 根据自己的业务设置对应的参数 */
AliRtcExternalAudioStreamConfig *config = [AliRtcExternalAudioStreamConfig new];
config.channels = _pcmChannels;
config.sampleRate = _pcmSampleRate;
config.playoutVolume = 0;
config.publishVolume = 100;
_externalPlayoutStreamId = [self.engine addExternalAudioStream:config];

Windows平台

/* 获取媒体引擎 */
IAliEngineMediaEngine* mAliRtcMediaEngine = nullptr;
    
mAliRtcEngine->QueryInterface(AliEngineInterfaceMediaEngine, (void **)&mAliRtcMediaEngine);
/* 根据自己的业务设置对应的参数 */
AliEngineExternalAudioStreamConfig config;
config.playoutVolume = currentAudioPlayoutVolume;
config.publishVolume = currentAudioPublishVolume;
config.channels = 1;
config.sampleRate = 48000;
config.publishStream = 0;
audioStreamID = mAliRtcMediaEngine->AddExternalAudioStream(config);

mAliRtcMediaEngine->Release();

向音频流内送入PCM数据

Android平台

AliRtcEngine.AliRtcAudioFrame rawData = new AliRtcEngine.AliRtcAudioFrame();
rawData.data = frameInfo.audio_data[0];
rawData.numSamples = (int) (frameInfo.audio_data[0].length / (2 * frameInfo.audio_channels));
rawData.bytesPerSample = 2;
rawData.numChannels = frameInfo.audio_channels;
rawData.samplesPerSec = frameInfo.audio_sample_rate;

int ret = mAliRtcEngine.pushExternalAudioStreamRawData(audioStreamID, rawData);

iOS平台

AliRtcAudioFrame *sample = [AliRtcAudioFrame new];
sample.dataPtr = _pcmLocalData;
sample.samplesPerSec = _pcmLocalSampleRate;
sample.bytesPerSample = sizeof(int16_t);
sample.numOfChannels = _pcmLocalChannels;
sample.numOfSamples = numOfSamples;
int rc = [self.engine pushExternalAudioStream:_externalPlayoutStreamId rawData:sample];

Windows平台

/* 获取媒体引擎 */
IAliEngineMediaEngine* mAliRtcMediaEngine = nullptr;
    
mAliRtcEngine->QueryInterface(AliEngineInterfaceMediaEngine, (void **)&mAliRtcMediaEngine);

AliEngineAudioRawData rawData;
rawData.dataPtr = frameInfo.audio_data[0];
rawData.numOfSamples = (int) (frameInfo.audio_data[0].length / (2 * frameInfo.audio_channels));
rawData.bytesPerSample = 2;
rawData.numOfChannels = frameInfo.audio_channels;
rawData.samplesPerSec = frameInfo.audio_sample_rate;
int ret = mAliRtcMediaEngine->PushExternalAudioStreamRawData(audioStreamID, rawData);
mAliRtcMediaEngine->Release();

移除外部音频流

Android平台

mAliRtcEngine.removeExternalAudioStream(audioStreamID);

iOS平台

[self.engine removeExternalAudioStream:_externalPublishStreamId];

Windows平台

/* 获取媒体引擎 */
IAliEngineMediaEngine* mAliRtcMediaEngine = nullptr;
    
mAliRtcEngine->QueryInterface(AliEngineInterfaceMediaEngine, (void **)&mAliRtcMediaEngine);

mAliRtcMediaEngine->RemoveExternalAudioStream(audioStreamID);
mAliRtcMediaEngine->Release();