Android: el código funciona con api 19 pero no con 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) } } 

/ * El error que está asociado con este código

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

Lo anterior es mi código para grabar un file de audio, funciona bien con dispositivos api 19 pero no funciona con api 24. ¿Me pueden ayudar?

Debe necesitar solicitar el permiso RECORD_AUDIO en time de ejecución para el nivel API 23 y superior.

Consulte este enlace para get más información.