我正在尝试官方链接的安卓语音记录应用程序,它让我在recorder.start();上崩溃,因为准备语句没有执行。
我在我的应用程序中所做的唯一改变就是我需要将音频记录存储在使用正式文档的位置。
// Record to the external cache directory for visibility
fileName = getExternalCacheDir().getAbsolutePath();
fileName += "/audiorecordtest.3gp";我的密码
String subfolder = "Exotel/Media/Audio/Voice Messages/Audio Temp";
String filename = "Exotel"+"_Voice"+System.currentTimeMillis()+".3gp";
String TempPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + File.separator + subfolder;
File Dir = new File(TempPath);
if (!Dir.exists()){
Dir.mkdirs();
}
TempPath = TempPath+File.separator+filename;
Log.d(TAG, "onTouch: Temp Path "+TempPath);
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(TempPath);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
recorder.prepare();
} catch (IOException e) {
Log.e(TAG, "prepare() failed "+e.getMessage());
}
recorder.start();如果我使用fileName = getExternalCacheDir().getAbsolutePath(); fileName += "/audiorecordtest.3gp";,那么我的代码就能很好地工作。另外,如果我将扩展更改为.mp3,那么它在本例中也能工作。我不明白我的代码到底出了什么问题。请帮帮忙。
发布于 2022-07-25 12:39:46
private var myAudioRecorder: MediaRecorder? = null
private var outputFile: String? = null在活动的setupAudioRecordSource onCreate中调用onCreate方法
private fun setupAudioRecordSource() {
outputFile = createPathToSaveAudio()
myAudioRecorder = MediaRecorder()
myAudioRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC)
myAudioRecorder?.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
myAudioRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
myAudioRecorder?.setOutputFile(outputFile)
}
private fun createPathToSaveAudio(): String? {
val storageDir = getAttachmentFilePath()
return File(
storageDir,
"recordedAudio_" + Calendar.getInstance().timeInMillis.toString() + ".3gp"
).absolutePath
}
fun Context.getAttachmentFilePath(): String {
val storageDir = File(
this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS),
"Attachment"
)
if (storageDir?.exists() == false) {
storageDir.mkdirs()
}
return storageDir.absolutePath
}当我开始记录下面的音频调用时
private fun recordAudio() {
try {
myAudioRecorder?.prepare()
myAudioRecorder?.start()
} catch (e: IllegalStateException) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (e: IOException) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}在“停止”按钮中记录下面的方法之后
private fun stopAudio() {
if (myAudioRecorder != null) {
try {
myAudioRecorder?.stop()
myAudioRecorder?.reset()
myAudioRecorder = null
} catch (e: java.lang.IllegalStateException) {
// it is called before start()
e.printStackTrace()
} catch (e: RuntimeException) {
// no valid audio/video data has been received
e.printStackTrace()
}
}
}https://stackoverflow.com/questions/73103741
复制相似问题