录制音频并随后播放 [英] Record audio and play it afterwards

查看:92
本文介绍了录制音频并随后播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote a class that shall make it possible to record an audio, save it to the sd card and then play the result. Here is my code:

 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中尝试过.按下停止按钮后,应保存音频并播放一次音频.我在做什么错了?

The recording works fine. The recorded file also appears on my sd card, just as I want it to be. But can't get the app to play this recorded audio. As you can see I tried it just in the BUTTON_POSITIV. After pressing the stop button the audio shall be saved and the audio played once. What am I doing wrong?

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

This is how I am calling the function:

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

推荐答案

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

Try passing the recorded file's descriptor to the setDataSource() method instead :

 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();
}

这篇关于录制音频并随后播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆