Android:默认录音

时间:2014-01-31 07:23:49

标签: android audio local-storage audio-recording android-sdcard

我正在尝试使用以下代码录制音频。

public void onClick(DialogInterface dialog, int id) {
    System.out.println("Inside Audio recording");
    File storageDir = new File(Environment.getExternalStorageDirectory()+ "/filetoupload/");
    if (!storageDir.exists()) {
        storageDir.mkdirs();
    }
    try {
            System.out.println("Inside Audio recording try block");
            imageToStore = new File(storageDir,"" + "audio.3gp");
            Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(imageToStore));
            System.out.println("Inside Audio recording path"+ imageToStore);
            startActivityForResult(intent,CAPTURE_AUDIO);
        } catch (Exception e) {
            e.printStackTrace();
            }
        }

}
}

它将打开默认的Android录制播放器,让我录制音频文件。

所以基本上它会启动默认音频录制,并且应该在SD卡中创建文件夹名称 filetoupload ,并且应该将其存储为名称 audio.3gp 但它不是这样做。它将记录存储在设备的默认记录文件夹中,或者使用其他名称,如 recording-10555454545.3gp

那么如何将此音频存储在SD卡特定文件夹中并使用 audio.3gp 名称请帮助。

1 个答案:

答案 0 :(得分:3)

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {

            if (requestCode == RQS_RECORDING) {
                try {

                    AssetFileDescriptor videoAsset = getContentResolver()
                            .openAssetFileDescriptor(data.getData(), "r");
                    FileInputStream fis;

                    fis = videoAsset.createInputStream();

                    File root = new File(Environment
            .getExternalStorageDirectory().getAbsolutePath() + "/Foldername/", "AUDIO"); // you
                    if (!root.exists()) {

                        root.mkdirs();
                    }
                    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                            .format(new Date());
                    File file;
                    file = new File(root.getPath() + "/" + "AUD_" + timeStamp
                            + ".mp3");
                    FileOutputStream fos = new FileOutputStream(file);

                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = fis.read(buf)) > 0) {
                        fos.write(buf, 0, len);
                    }
                    fis.close();
                    fos.close();
                Uri fileuri = Uri.fromFile(file);
                        System.out.println("you Audio path"+fileuri.getpath)


                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }


}