首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MediaRecorder.Prepare() IllegalStatEexception

MediaRecorder.Prepare() IllegalStatEexception
EN

Stack Overflow用户
提问于 2022-07-25 03:23:14
回答 1查看 55关注 0票数 -1

我正在尝试官方链接的安卓语音记录应用程序,它让我在recorder.start();上崩溃,因为准备语句没有执行。

我在我的应用程序中所做的唯一改变就是我需要将音频记录存储在使用正式文档的位置。

代码语言:javascript
复制
 // Record to the external cache directory for visibility
        fileName = getExternalCacheDir().getAbsolutePath();
        fileName += "/audiorecordtest.3gp";

我的密码

代码语言:javascript
复制
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,那么它在本例中也能工作。我不明白我的代码到底出了什么问题。请帮帮忙。

EN

回答 1

Stack Overflow用户

发布于 2022-07-25 12:39:46

代码语言:javascript
复制
private var myAudioRecorder: MediaRecorder? = null
private var outputFile: String? = null

在活动的setupAudioRecordSource onCreate中调用onCreate方法

代码语言:javascript
复制
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
}

当我开始记录下面的音频调用时

代码语言:javascript
复制
   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()
        }
    }

在“停止”按钮中记录下面的方法之后

代码语言:javascript
复制
   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()
            }
        }
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73103741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档