英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论