Android:代码适用于api 19但不适用于api 24

时间:2017-06-05 15:21:53

标签: android kotlin mediarecorder

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)
        }
    }

/ * 与此代码相关的错误

致命的例外:主要                                                                             处理:com.example.admin.lifeplus,PID:21679                                                                             java.lang.RuntimeException:setAudioSource失败。                                                                                 在android.media.MediaRecorder.setAudioSource(本机方法)                                                                                 在com.example.admin.lifeplus.record.MediaRecorderReady(record.kt:138)                                                                                 在com.example.admin.lifeplus.record $ onCreate $ 1.onClick(record.kt:45)                                                                                 在android.view.View.performClick(View.java:5623)                                                                                 在android.view.View $ PerformClick.run(View.java:22433)                                                                                 在android.os.Handler.handleCallback(Handler.java:751)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                 在android.os.Looper.loop(Looper.java:154)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:6247)                                                                                 at java.lang.reflect.Method.invoke(Native Method)                                                                                 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:872)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) * /

上面给出的是我录制音频文件的代码,它适用于api 19设备但不适用于api 24.你能帮助我吗?

1 个答案:

答案 0 :(得分:2)

您必须在运行时为API级别23及更高版本请求RECORD_AUDIO权限。

查看this链接了解详情。