Android Studio Kotlin: Why my Button continually causes crashes when I clicked it

63 views Asked by At

i am making a voice recorder activity, but when i press the button to play the recorded voice it give me a message "unable to play the audio file: /storage/emulated//0/recorded_audio.mp3: open..."

Here's the code:

val hintBar: TextView = findViewById(R.id.hint_bar)
        val filePath = "${Environment.getExternalStorageDirectory().absolutePath}/recorded_audio.mp3"
        val outputFile = File(filePath)
        recordingStatusTextView = findViewById(R.id.recording_status)
        startRecordingButton = findViewById(R.id.start_recording)
        stopRecordingButton = findViewById(R.id.stop_recording)

        mediaRecorder = MediaRecorder()
        mediaRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC)
        mediaRecorder?.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
        mediaRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
        mediaRecorder?.setOutputFile(outputFile.path)


        startRecordingButton.setOnClickListener {

            mediaRecorder = MediaRecorder().apply {
                setAudioSource(MediaRecorder.AudioSource.MIC)
                setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
                setOutputFile(filePath)
                setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)

                try {
                    mediaRecorder?.prepare()
                    mediaRecorder?.start()
                    recordingStatusTextView.text = "Recording..." // updating the status

                } catch (e: IllegalStateException) {
                    e.printStackTrace()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
                hintBar.visibility = View.VISIBLE
            }
        }

        stopRecordingButton.setOnClickListener{
            try {
                mediaRecorder?.stop()
                mediaRecorder?.release()
                recordingStatusTextView.text = "Recording Stopped" // updating the status

            } catch (e: IllegalStateException) {
                e.printStackTrace()
            } catch (e: IOException) {
                e.printStackTrace()
            }
            hintBar.visibility = View.GONE
        }



        lateinit var mediaPlayer: MediaPlayer
        val playButton = findViewById<Button>(R.id.play_button)
        playButton.setOnClickListener {
            val mediaPlayer = MediaPlayer()
            try {
                mediaPlayer.setDataSource(filePath)
                mediaPlayer.prepare()
                mediaPlayer.start()
            } catch (e: IOException) {
                // Show a toast message to the user
                Toast.makeText(this, "Unable to play the audio file: " + e.message, Toast.LENGTH_SHORT).show()
                Log.e("MediaPlayer", e.toString())
            }

        }

it should just play the voice that i just recorded but it's showing the "unable to play the audio file: /storage/emulated//0/recorded_audio.mp3: open..." message

0

There are 0 answers