Advertisement
ReyDahanCerio

Untitled

May 2nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.16 KB | None | 0 0
  1. // MARK: Audio Recording                                                        
  2.                                                                                
  3. private fun record() {                                                          
  4.                                                                                
  5.     recorder.setAudioSource(MediaRecorder.AudioSource.MIC)                      
  6.     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)              
  7.     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)                
  8.     val path: File = filesDir                                                  
  9.     try {                                                                      
  10.         tempFile = File.createTempFile("audioTemp", ".3gp", path)              
  11.     } catch (e: IOException) {                                                  
  12.         Log.d("recording error", "recording error:", e)                        
  13.     } catch (e: FileAlreadyExistsException) {                                  
  14.         Log.d("File already Exist", e.toString())                              
  15.     }                                                                          
  16.                                                                                
  17.     recorder.setOutputFile(tempFile?.absolutePath)                              
  18.     try {                                                                      
  19.         recorder.prepare()                                                      
  20.     } catch (e: IOException) {                                                  
  21.         Log.d("recording error", "recording error:", e)                        
  22.     }                                                                          
  23.     recorder.start()                                                            
  24. }                                                                              
  25.                                                                                
  26. private fun stopRecord() {                                                      
  27.     recorder.stop()                                                            
  28.     recorder.release()                                                          
  29.     button_play_sample.isEnabled = true                                        
  30.     button_record.isEnabled = true                                              
  31.     player.setOnCompletionListener(this)                                        
  32.     try {                                                                      
  33.         player.setDataSource(tempFile?.absolutePath)                            
  34.     } catch (e: IOException) {                                                  
  35.         Log.d("stop recording error", "Stop Recording Error:", e)              
  36.     }                                                                          
  37.     try {                                                                      
  38.         player.prepare()                                                        
  39.     } catch (e: IOException) {                                                  
  40.         Log.d("recording error", "recording error:", e)                        
  41.     }                                                                          
  42. }                                                                              
  43.                                                                                
  44. private fun play() {                                                            
  45.     player.start()                                                              
  46.     button_record.isEnabled = false                                            
  47. }                                                                              
  48.                                                                                
  49. override fun onCompletion(mp: MediaPlayer?) {                                  
  50.     handler = Handler()                                                        
  51.     handler?.postDelayed({button_record.isEnabled = true}, 1000)                
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement