I'm developing a record voice and play it application and I'm getting stuck. I want my application to record audio and when I tap on it should stop. It can record voice but after tap on button it crash. I've already checked all links related to it. I've trying to fix this error for the last 2 days. Help me please.
Here is my code
class AudioManager(private val context: Context) {
private var mediaRecorder: MediaRecorder? = null
private var mediaPlayer: MediaPlayer? = null
fun startPlayback(id: Int): Boolean {
val path = filePathForId(id)
if (File(path).exists()) {
mediaPlayer = MediaPlayer()
mediaPlayer?.setDataSource(path)
mediaPlayer?.prepare()
mediaPlayer?.start()
return true
}
return false
}
fun stopPlayback() {
mediaPlayer?.stop()
mediaPlayer?.release()
mediaPlayer = null
}
private fun filePathForId(id: Int): String {
// Once Kotlin has proper UInt type change this
return Environment.getExternalStorageDirectory().absolutePath + "/$id.aac"
}
fun startRecording(id: Int): Boolean {
//check the device has a microphone
if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_MICROPHONE)) {
//create new instance of MediaRecorder
mediaRecorder = MediaRecorder()
Toast.makeText(context, "Hold Button", Toast.LENGTH_SHORT).show()
//specify source of audio (Microphone)
mediaRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC)
//specify file type and compression format
mediaRecorder?.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS)
mediaRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
//specify audio sampling rate and encoding bit rate (48kHz and 128kHz respectively)
mediaRecorder?.setAudioSamplingRate(48000)
mediaRecorder?.setAudioEncodingBitRate(128000)
//specify where to save
val fileLocation = filePathForId(id)
mediaRecorder?.setOutputFile(fileLocation)
//record
mediaRecorder?.prepare()
mediaRecorder?.start()
return true
} else {
return false
}
}
fun stopRecording() {
try {
mediaRecorder?.stop()
mediaRecorder?.release()
mediaRecorder = null
} catch (e: Exception) {
e.printStackTrace()
}
}
}
And Error I'm getting
E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.io.IOException: setDataSourceFD failed.: status=0x80000000
at android.media.MediaPlayer._setDataSource(Native Method)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1382)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1365)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1296)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1233)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1200)
at com.mudasir.happijobs.utils.AudioManager.startPlayback(AudioManager.kt:25)
at com.mudasir.happijobs.ui.fragments.findlabour.AddJobFragment.startRecording(AddJobFragment.kt:187)
at com.mudasir.happijobs.ui.fragments.findlabour.AddJobFragment.access$startRecording(AddJobFragment.kt:43)
at com.mudasir.happijobs.ui.fragments.findlabour.AddJobFragment$askAudioPermission$1.onPermissionsChecked(AddJobFragment.kt:129)
at com.karumi.dexter.DexterInstance$1.run(Unknown Source:43)
at com.karumi.dexter.MainThread.execute(Unknown Source:6)
at com.karumi.dexter.DexterInstance.checkMultiplePermissions(Unknown Source:71)
at com.karumi.dexter.DexterInstance.checkPermissions(Unknown Source:0)
at com.karumi.dexter.Dexter.check(Unknown Source:10)
at com.mudasir.happijobs.ui.fragments.findlabour.AddJobFragment.askAudioPermission(AddJobFragment.kt:139)
at com.mudasir.happijobs.ui.fragments.findlabour.AddJobFragment.access$askAudioPermission(AddJobFragment.kt:43)
at com.mudasir.happijobs.ui.fragments.findlabour.AddJobFragment$onCreateView$2.onTouch(AddJobFragment.kt:74)
at android.view.View.dispatchTouchEvent(View.java:12624)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3041)
User contributions licensed under CC BY-SA 3.0