我停止录制后无法找到录制的文件

时间:2013-07-30 22:33:10

标签: java android eclipse android-mediarecorder

我正在制作一个简单的应用程序,只要触摸并保持图像按钮就可以录制声音,但在录制完成后,我无法在Android设备上找到该文件。

代码:

public class MainActivity extends Activity {

MediaRecorder recorder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //AUDIO RECORDER
    recorder = new MediaRecorder();
    recorder.reset();       
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
        String externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        recorder.setOutputFile(externalStoragePath + "/easyvoicenotepad/test.3gp");
    }
    else
    {
        String storagePath = Environment.getRootDirectory().getAbsolutePath();
        recorder.setOutputFile(storagePath + "/easyvoicenotepad/test.3gp");
    }

    //IMAGE BUTTON ONTOUCHLISTENER
    final ImageButton recBtn = (ImageButton) findViewById(R.id.recButton);
    recBtn.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                recBtn.setImageResource(R.drawable.record_btn_pressed);
                try {
                    recorder.prepare();
                    recorder.start();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else if (event.getAction() == MotionEvent.ACTION_UP)
            {
                recBtn.setImageResource(R.drawable.record_btn);
                try {
                    recorder.prepare();
                    recorder.stop();
                    recorder.reset();
                    recorder.release();
                    recorder = null;
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return true;
        }

    });
} //END OF ONCREATE

权限:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.STORAGE" />

2 个答案:

答案 0 :(得分:0)

如果您将设备连接到计算机,并且正在其中搜索该文件,请尝试断开连接并重新连接设备。我发现你在开发过程中创建的文件并不总是立即出现,尽管它们确实存在。

添加快速检查以确保在您编写文件后,如果您怀疑该文件不是出于任何原因,该文件确实存在于外部存储上。

答案 1 :(得分:0)

您需要首先创建您的目录,在onCreate方法

中添加这样的内容
 boolean exists = false;
 File createdir = new File(Environment.getExternalStorageDirectory() + "/easyvoicenotepad");
 File createdir2 = new File(Environment.getRootDirectory().getAbsolutePath() +"/easyvoicenotepad");
    if (createdir.exists()) {       
    exists = true;
    }
    else {
        createdir.mkdir();
    }
    if (createdir2.exists()) {      
    exists = true;
    }
    else {
        createdir.mkdir();
    }