【问题标题】:Record audio and play it afterwards录制音频并在之后播放
【发布时间】:2015-03-10 18:24:24
【问题描述】:

我写了一个类,可以录制音频,将其保存到 sd 卡,然后播放结果。这是我的代码:

 public void recordAudio(final String fileName) {
        final MediaRecorder recorder = new MediaRecorder();
        recorder.reset();
        ContentValues values = new ContentValues(3);
        values.put(MediaStore.MediaColumns.TITLE, fileName);
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        final String myPath = Environment.getExternalStorageDirectory() + "/MyOwnQuiz/" + fileName + ".3gp";
        recorder.setOutputFile(myPath);
        try {
            recorder.prepare();
        } catch (Exception e){
            e.printStackTrace();
        }

        final ProgressDialog mProgressDialog = new ProgressDialog(getActivity());
        mProgressDialog.setTitle("Record audio");
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setButton (DialogInterface.BUTTON_POSITIVE,"Stop recording", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                mProgressDialog.dismiss();
                recorder.stop();
                recorder.release();

                MediaPlayer mp = new MediaPlayer();

                File f = new File(myPath);
                if(f.exists()){
                    try{
                        mp.setDataSource(myPath);
                        mp.prepare();
                    }catch(IOException e){

                    }
                    mp.start();
                }

            }
        });

        mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
            public void onCancel(DialogInterface p1) {
                recorder.stop();
                recorder.release();
            }
        });
        recorder.start();
        mProgressDialog.show();


    }

录音工作正常。录制的文件也出现在我的 SD 卡上,就像我想要的那样。但无法让应用播放此录制的音频。如您所见,我只是在 BUTTON_POSITIV 中尝试过。按下停止按钮后,音频将被保存并播放一次。我做错了什么?

这就是我调用函数的方式:

Calendar myCal = Calendar.getInstance();
                            String myString = myCal.getTime().toString();
                            recordAudio(myString);

【问题讨论】:

  • if(f.exists()) 返回 true 吗?
  • 再次检查:是的,确实如此。

标签: android android-mediaplayer android-sdcard


【解决方案1】:

尝试将记录的文件的描述符传递给 setDataSource() 方法:

 File f = new File(myPath);
                if(f.exists()){
 FileInputStream fis = new FileInputStream(f);
 FileDescriptor fileD = fis.getFD();
                 try{
                        mp.setDataSource(fileD);
                        mp.prepare();
                    }catch(IOException e){

                    }
                    mp.start();
}

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多