2017-06-05 37 views
0
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) 
     } 
    } 

/* 與此代碼機器人:代碼可與API 19,但不與API 24

致命異常相關聯的錯誤:主 工藝:com.example.admin.lifeplus,PID: 21679 java.lang.RuntimeException:setAudioSource失敗。 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) */

上面給出的是我用於錄製音頻文件的代碼,它適用於api 19設備,但不適用於api 24.你能幫我嗎?

+1

你是否要求在運行時的RECORD_AUDIO權限? – AlexTa

+0

是的,我包含在AndroidManifest文件中,我檢查了沒有權限的相同代碼 –

+0

存在相同的錯誤 –

回答

2

您必須在運行時爲API級別23和更高級別請求RECORD_AUDIO權限。

檢查出this鏈接獲取更多信息。

+0

所以我必須添加每個音頻記錄的權限代碼 –