title: My application with mediacord is destroyed
body: <p>I am trying to create an application that allows me to record audio, save audio and play recorded audio.</p>
  <p>The problen :</p>
  <p>When I press the button to record or play my application is destroyed.</p>
  <p>i have looked that video but i don´t undertan what happen</p>
  <pre><code>&lt;!--https://www.youtube.com/watch?v=_Z0AZoTxWYQ--&gt;
  </code></pre>
  <p>I have added this in my manifest</p>
  <pre><code>   &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;
         &lt;uses-permission android:name=&quot;android.permission.RECORD_AUDIO&quot;/&gt;
  </code></pre>
  <pre><code>  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() + &quot;/grabacion.mp3&quot;
          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
   &lt;!--BUTTON TO RECORD--&gt;
          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
          }
  
          &lt;!--BUTTON STOP--&gt;
  
          button2.setOnClickListener {
              recorder.stop()
              button.isEnabled = true
              button2.isEnabled = false
          }
  
          &lt;!--BUTTON PLAY--&gt;
  
          button3.setOnClickListener {
              val mediaPlayer = MediaPlayer()
              mediaPlayer.setDataSource(path)
              mediaPlayer.prepare()
              mediaPlayer.start()
      
              }
      }
  }
  </code></pre>