Android平台GB28181设备接入端语音广播如何实现实时音量调节

2022-10-27 11:06:03 浏览数 (1)

​Android平台GB28181设备接入,语音广播功能非常重要,本文要介绍的,不是语音广播的流程,语音广播流程,之前的blog也有非常详细的分享,感兴趣的可以参考官方规范书的交互流程:

语音广播这块,有开发者提出这样的诉求,如何适时调节语音广播的音量?

因为我们的语音广播,audio的播放,是基于我们RTMP、RTSP直播播放模块设计实现的,对应的接口如下:

代码语言:javascript复制
	/**
	 * 设置播放音量
	 *
	 * @param handle: return value from SmartPlayerOpen()
	 *
	 * @param volume: 范围是[0, 100], 0是静音,100是最大音量, 默认是100
	 *
	 * @return {0} if successful
	 */
	public native int SmartPlayerSetAudioVolume(long handle, int volume);

如果需要实时调节音量,只要加个音量调节的SeekBar即可:

代码语言:javascript复制
        audioVolumeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

                curAudioVolume = seekBar.getProgress();

                audioVolumeText.setText("当前音量: "   curAudioVolume);

                if(playerHandle != 0)
                {
                    libPlayer.SmartPlayerSetAudioVolume(playerHandle, curAudioVolume);
                }
            }
        });

如果需要在启动播放的时候,给个默认的初始音量,可以在StartAudioPlay()的时候,设置即可:

代码语言:javascript复制
    private boolean startAudioPlay() {
        if (player_handle_ != 0 )
            return false;

        player_handle_ = lib_player_.SmartPlayerOpen(context_);
        if (player_handle_ == 0)
            return false;

        lib_player_.SetSmartPlayerEventCallbackV2(player_handle_,new EventHandePlayerV2());

        // 缓存大小可以调整
        lib_player_.SmartPlayerSetBuffer(player_handle_, 0);

        // lib_player_.SmartPlayerSetFastStartup(player_handle_, 0);

        // set report download speed(默认2秒一次回调 用户可自行调整report间隔)
        lib_player_.SmartPlayerSetReportDownloadSpeed(player_handle_, 1, 20);

        lib_player_.SmartPlayerClearRtpReceivers(player_handle_);
        lib_player_.SmartPlayerAddRtpReceiver(player_handle_, rtp_receiver_handle_);

        lib_player_.SmartPlayerSetSurface(player_handle_, null);
        // lib_player_.SmartPlayerSetRenderScaleMode(player_handle_, 1);

        lib_player_.SmartPlayerSetAudioOutputType(player_handle_, 1);

        lib_player_.SmartPlayerSetMute(player_handle_, 0);

        lib_player_.SmartPlayerSetAudioVolume(player_handle_, 100);

        lib_player_.SmartPlayerSetExternalAudioOutput(player_handle_, new PlayerExternalPCMOutput());

        lib_player_.SmartPlayerSetUrl(player_handle_, "rtp://ntinternal/rtpreceiver/implemention0");

        if (0 != lib_player_.SmartPlayerStartPlay(player_handle_)) {
            lib_player_.SmartPlayerClose(player_handle_);
            player_handle_ = 0;

            Log.e(TAG,  "start audio paly failed");
            return false;
        }

        lib_player_.SmartPlayerSetAudioDataCallback(player_handle_, new PlayerAudioDataOutput());
        lib_player_.SmartPlayerSetPullStreamAudioTranscodeAAC(player_handle_, 0);

        if (0 ==lib_player_.SmartPlayerStartPullStream(player_handle_) ) {
            // 启动定时器,长时间收不到音频数据,则停止播放,发送BYE
            last_received_audio_data_time_.set(SystemClock.elapsedRealtime());
            handler_.postDelayed(new AudioPlayerPCMTimer(player_handle_), AudioPlayerPCMTimer.INTERVAL_MS);
        }

        return true;
    }

如果需要静音,audio volume设置为0即可,以上是大概的接口设计和调用逻辑,感兴趣的开发者,可以自行尝试。

0 人点赞