当我尝试在Android Studio中录制音频时出现此错误,我已经在清单中设置了“ Record_Audio”和“ Write_External_Storage”权限,但它仍然出现。 在方法“ grabar”的第一个捕获中出现的错误是“ errorjava.io.IOException:打开失败:EACCES(权限被拒绝)”。
lateinit var recorder: MediaRecorder
lateinit var player: MediaPlayer
var archivo: File? =null
val RECORD_AUDIO:Int = 0;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
boton1.setOnClickListener {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.RECORD_AUDIO), RECORD_AUDIO)
}else{
grabar()
}
}
private fun grabar() {
recorder = MediaRecorder()
recorder.setAudioSource(MediaRecorder.AudioSource.MIC)
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
val path = File(Environment.getExternalStorageDirectory().getPath())
try {
archivo = File.createTempFile("temporal", ".3gp", path)
} catch (e: IOException) {//ERROR
e.toString()
println("Este es el error"+e)
}
recorder.setOutputFile(archivo!!.absolutePath)
try {
recorder.prepare()
} catch (e: IOException) {
}
recorder.start()
tv1.text ="Grabando"
boton1.setEnabled(false)
boton2.setEnabled(true)
}