仅广播原始音频数据,而不使用麦克风或扬声器

时间:2019-01-04 17:49:02

标签: android webrtc live android-broadcast agora.io

我只能使用麦克风和扬声器单独广播音频,如果我使用setExternalAudioSource方法,则广播时会遇到一些不必要的噪音。我只想单独广播原始音频数据,而不使用麦克风,扬声器和不必要的噪音。

 private val PERMISSION_REQ_ID_RECORD_AUDIO = 22
private var mRtcEngine: RtcEngine? = null// Tutorial Step 1
private val mRtcEventHandler = object : IRtcEngineEventHandler() { // Tutorial Step 1

    override fun onUserOffline(uid: Int, reason: Int) { // Tutorial Step 4
        //runOnUiThread { onRemoteUserLeft(uid, reason) }
    }

    override fun onUserMuteAudio(uid: Int, muted: Boolean) { // Tutorial Step 6
        // runOnUiThread { onRemoteUserVoiceMuted(uid, muted) }
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    if (checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO)) {
        createRtcChannel()
    }

}

fun checkSelfPermission(permission: String, requestCode: Int): Boolean {
    if (ContextCompat.checkSelfPermission(this,
                    permission) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this,
                arrayOf(permission),
                requestCode)
        return false
    }
    return true
}

private fun createRtcChannel() {
    initializeAgoraEngine()     // Tutorial Step 1
    joinChannel()
}

private fun initializeAgoraEngine() {
    try {
        mRtcEngine = RtcEngine.create(this, getString(R.string.agora_app_id), mRtcEventHandler)
        //set the channel as live broadcast mode
        mRtcEngine?.setChannelProfile(Constants.CHANNEL_PROFILE_LIVE_BROADCASTING)
        mRtcEngine?.setClientRole(Constants.CLIENT_ROLE_BROADCASTER)
    } catch (e: Exception) {

    }
}

private fun joinChannel() {
    mRtcEngine?.joinChannel(null, "voiceDemoChannel1", "Extra Optional Data", 0) // if you do not specify the uid, we will generate the uid for you

    val payload = IOUtils.toByteArray(assets.openFd("ringtone.mp3").createInputStream())

    mRtcEngine?.setExternalAudioSource(
            true,      
            8000,     
            1     );

    mRtcEngine?.pushExternalAudioFrame(
            payload,
            1000
    )

}

使用agora还是有可能吗?

1 个答案:

答案 0 :(得分:0)

原因可能会引起噪音:

  1. 您的源音频PCM样本本身很吵
  2. 引擎错误
  3. 您设置的采样率错误

对于第一个,您可以直接检查PCM样本。对于第二种方法,由于已经有很多人在使用,因此很少是真的。因此,如果您确定您的源PCM样本是好的,我将建议您检查采样率。

此外,您可以在加入频道之前设置APM选项,以通过以下方式为外部源启用音频增强功能:

setParameters("{\"che.audio.override_apm\":true}")