我的应用程序与 Mediacord 被销毁。

huangapple go评论74阅读模式
英文:

My application with mediacord is destroyed

问题

class MainActivity : AppCompatActivity() {
    lateinit var recorder: MediaRecorder
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val path: String = Environment.getExternalStorageDirectory().toString() + "/grabacion.mp3"
        recorder = MediaRecorder()

        button.isEnabled = false
        button2.isEnabled = false

        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.RECORD_AUDIO
            ) != PackageManager.PERMISSION_GRANTED
        )
            ActivityCompat.requestPermissions(
                this, arrayOf(
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ), 111
            )

        button.isEnabled = true

        button.setOnClickListener {
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC)
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
            recorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB)
            recorder.setOutputFile(path)
            recorder.prepare()
            recorder.start()

            button2.isEnabled = true
            button.isEnabled = false
        }

        button2.setOnClickListener {
            recorder.stop()
            button.isEnabled = true
            button2.isEnabled = false
        }

        button3.setOnClickListener {
            val mediaPlayer = MediaPlayer()
            mediaPlayer.setDataSource(path)
            mediaPlayer.prepare()
            mediaPlayer.start()
        }
    }
}
英文:

I am trying to create an application that allows me to record audio, save audio and play recorded audio.

The problen :

When I press the button to record or play my application is destroyed.

i have looked that video but i don´t undertan what happen

<!--https://www.youtube.com/watch?v=_Z0AZoTxWYQ-->

I have added this in my manifest

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
       <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  class MainActivity : AppCompatActivity() {
        lateinit var recorder: MediaRecorder
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val path: String = Environment.getExternalStorageDirectory().toString() + "/grabacion.mp3"
        recorder = MediaRecorder()

        button.isEnabled = false
        button2.isEnabled = false

        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.RECORD_AUDIO
            ) != PackageManager.PERMISSION_GRANTED
        )
            ActivityCompat.requestPermissions(
                this, arrayOf(
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ), 111
            )


        button.isEnabled = true
 <!--BUTTON TO RECORD-->
        button.setOnClickListener {
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC)
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
            recorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB)
            recorder.setOutputFile(path)
            recorder.prepare()
            recorder.start()

            button2.isEnabled = true
            button.isEnabled = false
        }

        <!--BUTTON STOP-->

        button2.setOnClickListener {
            recorder.stop()
            button.isEnabled = true
            button2.isEnabled = false
        }

        <!--BUTTON PLAY-->

        button3.setOnClickListener {
            val mediaPlayer = MediaPlayer()
            mediaPlayer.setDataSource(path)
            mediaPlayer.prepare()
            mediaPlayer.start()
    
            }
    }
}

答案1

得分: 0

我已找到解决方案,引起问题的原因是

val path: String = Environment.getExternalStorageDirectory().toString() + "/grabacion.mp3";

我们需要将其更改为

val path: String = getExternalFilesDir(null).toString() + "/grabacion.mp3";
英文:

I have found the solution,what was causing the problem was

val path: String = Environment.getExternalStorageDirectory().toString() + "/grabacion.mp3"

We need to change it for

val path: String = getExternalFilesDir(null).toString() + "/grabacion.mp3"

huangapple
  • 本文由 发表于 2020年7月27日 05:51:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63106097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定