How can i save the audio record on Firebase?


#1

Hello i’m taking a audio record on my project but i want to save this records on firebase database with username and topic name how can i do this ?

My code is taking a 60 seconds audio record and after that its stopped the record.

Here is my record save codes.,

     class MainActivity : AppCompatActivity(), MediaPlayer.OnCompletionListener {
    lateinit var recorder: MediaRecorder
      lateinit var player: MediaPlayer
     lateinit var file: File
    lateinit var button1: Button
  lateinit var button2: Button
   lateinit var button3: Button
     lateinit var saveButton: Button
    lateinit var tv1: TextView
   lateinit var timeText : TextView
   lateinit var topicText: EditText

 var handler: Handler = Handler()
 var runnbale: Runnable = Runnable {  }
 var cihan: CountDownTimer? = null
 val fbsignup = FirebaseAuth.getInstance()
 lateinit var mDatabase:DatabaseReference
 var user = FirebaseAuth.getInstance().currentUser
 private var mFileName = null


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

val nameText = findViewById<View>(R.id.nameText) as TextView
var uid = user!!.uid
mDatabase =FirebaseDatabase.getInstance().getReference("Nicks")

mDatabase.child(uid).child("Nick").addValueEventListener(object : ValueEventListener{
    override fun onCancelled(p0: DatabaseError?) {

    }

    override fun onDataChange(snapshot: DataSnapshot) {

        nameText.text ="Welcome " + snapshot.value.toString()
    }
})


    val intent = intent


tv1 = findViewById (R.id.tv1) as TextView
button1 = findViewById (R.id.button1) as Button
button2 = findViewById (R.id.button2) as Button
button3 = findViewById (R.id.button3) as Button
topicText =findViewById(R.id.topicText)
saveButton=findViewById(R.id.saveButton)

timeText = findViewById(R.id.timeText) as TextView



var button2clk: View.OnClickListener = View.OnClickListener{
    cihan?.cancel()
    recorder.stop ()
    recorder.release ()
    player = MediaPlayer ()


    player.setOnCompletionListener (this)



    try {
        player.setDataSource(file.absolutePath)
    } catch (e: IOException) {
    }

    try {
        player.prepare ()
    } catch (e: IOException) {
    }


    button1.setEnabled (true)
    button2.setEnabled (false)
    button3.setEnabled (true)
    tv1.text = "Ready to play"

    timeText.text ="Done!"

}
button1.setOnClickListener {
    if (topicText.text.toString().length == 0){
        button1.isClickable=false
        Toast.makeText(this, "The topic must be Entered!!",Toast.LENGTH_LONG).show()
    }

    recorder = MediaRecorder()
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC)
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
    val path = File (Environment.getExternalStorageDirectory().getPath())



    try {
        file = File.createTempFile ("temporary", ".mp3", path)
    } catch (e: IOException) {
    }

    recorder.setOutputFile(file.absolutePath)
    try {
        recorder.prepare ()
    } catch (e: IOException) {
    }

    recorder.start()
    tv1.text = "Recording"
    button1.setEnabled(false)
    button2.setEnabled(true)
    button3.setEnabled(false)

    cihan = object : CountDownTimer(60000,1000) {
        override fun onFinish(){
            timeText.text = "Time's Finish!"
            button2clk.onClick(button2)

        }

        override fun onTick(p0: Long) {
            timeText.text = "Time: "+ p0/1000 +" Sec"
        }
    }.start()

    }



button2.setOnClickListener(button2clk)


button3.setOnClickListener {
    player.start();
    button1.setEnabled (false);
    button2.setEnabled (false);
    button3.setEnabled (false);
    tv1.setText ("Playing");
}
}



override fun onCompletion (mp: MediaPlayer) {
button1.setEnabled (true)
button2.setEnabled (false)
button3.setEnabled (true)
tv1.setText ("Ready")
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu,menu)
return super.onCreateOptionsMenu(menu)
 }

what am i have to do for saving records on firebase ?