第二次按下录制按钮,我的应用程序崩溃了

时间:2018-05-02 07:31:55

标签: android kotlin

这是我的代码段。

private fun record() {                                                          

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC)                      
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)              
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)                
    val path: File = filesDir                                                  
    try {                                                                      
        tempFile = File.createTempFile("audioTemp", ".3gp", path)              
    } catch (e: IOException) {                                                  
        Log.d("recording error", "recording error:", e)                        
    } catch (e: FileAlreadyExistsException) {                                  
        Log.d("File already Exist", e.toString())                              
    }                                                                          

    recorder.setOutputFile(tempFile?.absolutePath)                              
    try {                                                                      
        recorder.prepare()                                                      
    } catch (e: IOException) {                                                  
        Log.d("recording error", "recording error:", e)                        
    }                                                                          
    recorder.start()                                                            
}                                                                              

private fun stopRecord() {                                                      
    recorder.stop()                                                            
    recorder.release()                                                          
    button_play_sample.isEnabled = true                                        
    button_record.isEnabled = true                                              
    player.setOnCompletionListener(this)                                        
    try {                                                                      
        player.setDataSource(tempFile?.absolutePath)                            
    } catch (e: IOException) {                                                  
        Log.d("stop recording error", "Stop Recording Error:", e)              
    }                                                                          
    try {                                                                      
        player.prepare()                                                        
    } catch (e: IOException) {                                                  
        Log.d("recording error", "recording error:", e)                        
    }                                                                          
}                                                                              

private fun play() {                                                            
    player.start()                                                              
    button_record.isEnabled = false                                            
}                                                                              

override fun onCompletion(mp: MediaPlayer?) {                                  
    handler = Handler()                                                        
    handler?.postDelayed({button_record.isEnabled = true}, 1000)                
}

我无法弄清楚为什么,但我有一个记录音频的录音按钮。当活动首次加载时,记录加载。当我第二次按它时,因为我不喜欢我的第一次录制,而不是覆盖旧文件并再次设置记录,而App崩溃了。

1 个答案:

答案 0 :(得分:3)

Android Developer Guide中,它说

  

完成MediaRecorder实例后,释放其资源   尽快通过调用release()。

请参阅开发者网站上的sample code

private void stopRecording() {
    recorder.stop();
    recorder.release();
    recorder = null;
}

您的代码段不会检查是否正在使用recorder。您可能希望在再次使用MediaRecorder对象之前执行此操作。

private fun record() {
    if (recording) {
        stopRecording()
    }

    // do recording snippet here...
}