blesson Samuel
blesson Samuel

Reputation: 421

Android: code works with api 19 but not with api 24

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.media.MediaPlayer
import android.media.MediaRecorder
import android.os.Environment
import android.view.View
import android.widget.Toast
import java.io.IOException
import android.widget.ImageButton


class record : AppCompatActivity() {

    var buttonStart: ImageButton? = null
    var buttonStop: ImageButton? = null
    var buttonPlayLastRecordAudio: ImageButton? = null
    var buttonStopPlayingRecording: ImageButton? = null
    var AudioSavePathInDevice = "Blesson"
    var mediaRecorder: MediaRecorder? = null


    var mediaPlayer: MediaPlayer? = null

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

        buttonStart = findViewById(R.id.record) as ImageButton
        buttonStop = findViewById(R.id.stop) as ImageButton
        buttonPlayLastRecordAudio = findViewById(R.id.play) as ImageButton
        buttonStopPlayingRecording= findViewById(R.id.playstop) as ImageButton
        buttonStop!!.isEnabled = false
        buttonPlayLastRecordAudio!!.isEnabled = false




        buttonStart!!.setOnClickListener {


            AudioSavePathInDevice = Environment.getExternalStorageDirectory().absolutePath + "/" +"Medpro.mp3"

            MediaRecorderReady()

            try {
                mediaRecorder!!.prepare()
                mediaRecorder!!.start()
            } catch (e: IllegalStateException) {
                Toast.makeText(this, "Recording roblem started",
                        Toast.LENGTH_LONG).show()
            }



            buttonStart!!.isEnabled = false
            buttonStart!!.visibility = View.INVISIBLE
            buttonStop!!.visibility = View.VISIBLE
            buttonStop!!.isEnabled = true

            Toast.makeText(this, "Recording started",
                    Toast.LENGTH_LONG).show()

        }
            buttonStop!!.setOnClickListener {

                 mediaRecorder!!.stop()
                 buttonStop!!.isEnabled = false
                 buttonPlayLastRecordAudio!!.isEnabled = true
                // buttonStart!!.isEnabled = true
                 buttonStopPlayingRecording!!.isEnabled = false

                     Toast.makeText(this, "Recording Completed",
                            Toast.LENGTH_LONG).show()
                 buttonStop!!.visibility = View.INVISIBLE
                  buttonPlayLastRecordAudio!!.visibility = View.VISIBLE


        }

              buttonPlayLastRecordAudio!!.setOnClickListener {
                  buttonStop!!.isEnabled = false
                  buttonStart!!.isEnabled = false
                  buttonPlayLastRecordAudio!!.visibility=View.INVISIBLE
                  buttonStopPlayingRecording!!.isEnabled = true
                  buttonStopPlayingRecording!!.visibility=View.VISIBLE

                    mediaPlayer = MediaPlayer()
                     try {
                         mediaPlayer!!.setDataSource(AudioSavePathInDevice)
                         mediaPlayer!!.prepare()
                        } catch (e: IOException) {
                                Toast.makeText(this, "Recoring not found",
                                 Toast.LENGTH_LONG).show() 
                        }

                          mediaPlayer!!.start()
                          Toast.makeText(this, "Recording Playing",
                          Toast.LENGTH_LONG).show() 
              }

                 buttonStopPlayingRecording!!.setOnClickListener {

                 buttonStopPlayingRecording!!.isEnabled = false
                 buttonPlayLastRecordAudio!!.isEnabled = true

                      if (mediaPlayer != null) {
                      mediaPlayer!!.stop()
                      mediaPlayer!!.release()
                     MediaRecorderReady() 
                      }
              }



        }


        fun MediaRecorderReady() {
            mediaRecorder = MediaRecorder()
            mediaRecorder!!.setAudioSource(MediaRecorder.AudioSource.MIC)
            mediaRecorder!!.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
            mediaRecorder!!.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB)
            mediaRecorder!!.setOutputFile(AudioSavePathInDevice)
        }
    }

/* The error that is associated with this code

FATAL EXCEPTION: main Process: com.example.admin.lifeplus, PID: 21679 java.lang.RuntimeException: setAudioSource failed. at android.media.MediaRecorder.setAudioSource(Native Method) at com.example.admin.lifeplus.record.MediaRecorderReady(record.kt:138) at com.example.admin.lifeplus.record$onCreate$1.onClick(record.kt:45) at android.view.View.performClick(View.java:5623) at android.view.View$PerformClick.run(View.java:22433) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6247) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) */

The given above is my code for recording an audio file, it works fine with api 19 devices but does not work with api 24. Can you help me?

Upvotes: 0

Views: 382

Answers (1)

AlexTa
AlexTa

Reputation: 5251

You must need to ask for RECORD_AUDIO permission at runtime for API level 23 and above.

Check out this link for more information.

Upvotes: 2

Related Questions