RTMP推送端如何实时调整采集端音量
好多开发者提到,有些Windows机器或Android手机采集麦克风的音频,过小或过大,一般过小居多,还有就是,混音的时候,希望背景音音量大小可实时调整,针对这种情况,大牛直播SDK(Github)做了如下的设计:
1. Windows平台推送端(以C#为例):
/* *设置输入音量, 这个接口一般不建议调用, 在一些特殊情况下可能会用, 一般不建议放大音量 *index: 一般是0和1, 如果没有混音的只用0, 有混音的话, 0,1分别设置音量 *volume: 音量,默认是1.0,范围是[0.0, 5.0], 设置成0静音, 1音量不变 *成功返回 NT_ERC_OK */ [DllImport(@"SmartPublisherSDK.dll")] public static extern UInt32 NT_PB_SetInputAudioVolume(IntPtr handle, Int32 index, float volume);
对应界面如下:
![](https://ask8088-private-1251520898.cn-south.myqcloud.com/developer-images/article/1924601/77wrlmor7w.png?q-sign-algorithm=sha1&q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&q-sign-time=1592230270;1592237470&q-key-time=1592230270;1592237470&q-header-list=&q-url-param-list=&q-signature=37a84be6434abba79d67f9d7aff36ac665ffd7c6)
2. Android推送端:
/** * 设置输入音量, 这个接口一般不建议调用, 在一些特殊情况下可能会用, 一般不建议放大音量 * * @param index: 一般是0和1, 如果没有混音的只用0, 有混音的话, 0,1分别设置音量 * * @param volume: 音量,默认是1.0,范围是[0.0, 5.0], 设置成0静音, 1音量不变 * * @return {0} if successful */ public native int SmartPublisherSetInputAudioVolume(long handle, int index, float volume);
对应调用实例:
in_audio_volume_selector_ = (Spinner) findViewById(R.id.in_audio_volume_selector); final String[] in_audio_volume_sel = new String[]{ "0", "0.2", "0.5", "0.8","1", "1.5", "2", "2.5", "3", "3.5", "4", "4.5", "5"}; ArrayAdapter<String> adapter_in_audio_volume = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, in_audio_volume_sel); adapter_in_audio_volume.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); in_audio_volume_selector_.setAdapter(adapter_in_audio_volume); in_audio_volume_selector_.setSelection(4, true); in_audio_volume_selector_.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Log.i(TAG, "Currently audio volume choosing: " + in_audio_volume_sel[position]); in_audio_volume_ = Float.parseFloat(in_audio_volume_sel[position]); Log.i(TAG, "Choose audio volume=" + in_audio_volume_); if(isPushingRtmp || isRecording || isRTSPPublisherRunning || isPushingRtsp) { if (libPublisher != null && publisherHandle != 0) { libPublisher.SmartPublisherSetInputAudioVolume(publisherHandle, 0 , in_audio_volume_); } } } @Override public void onNothingSelected(AdapterView<?> parent) { } });
音频放大或缩小,需要注意的是,处理尽可能的线性,防止溢出,感兴趣的开发者,可以多交流。