Kotlin语音记录上传覆盖Firebase

时间:2018-01-06 10:43:03

标签: kotlin firebase-storage

Hello guysi写了一些代码来获取保存在firebase上的音频记录。它的工作,但始终保持相同的名称,因此它导致了覆盖。我该如何解决这个问题?

  mFileName = externalCacheDir!!.absolutePath
    mFileName += "/recorded_audio.3gp"


     private fun startRecording() {
    mRecorder = MediaRecorder()
    mRecorder!!.setAudioSource(MediaRecorder.AudioSource.MIC)
    mRecorder!!.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
    mRecorder!!.setOutputFile(mFileName)
    mRecorder!!.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)

    try {
        mRecorder!!.prepare()
    } catch (e: IOException) {
        Log.e(LOG_TAG, "prepare() failed")
    }

    mRecorder!!.start()
}

private fun stopRecording() {
    mRecorder!!.stop()
    mRecorder!!.release()
    mRecorder = null
    uploadAudio()
}

private fun uploadAudio() {

    val filepath = mStorage!!.child("Audio").child("new_audio.3gp")
    val uri = Uri.fromFile(File(mFileName!!))
    filepath.putFile(uri).addOnSuccessListener { }

}

companion object {
    private val LOG_TAG = "Record_log"
}

1 个答案:

答案 0 :(得分:1)

问题是您始终使用相同的文件名:

mFileName = externalCacheDir!!.absolutePath
mFileName += "/recorded_audio.3gp"

如果您生成随机UUID(或类似),请执行以下操作:

mFileName = externalCacheDir!!.absolutePath
mFileName += "/" + UUID.randomUUID().toString() + ".3gp"

您将不再覆盖文件。