在Android上保存录制的声音

时间:2015-01-08 18:58:14

标签: android audio-recording

我使用意图录制声音。

但我不知道如何在sdcard中保存这个声音。 我创建了一个文件夹" file.mkdirs();"。如何捕获记录并保存在新文件夹中(并更改名称)?

如果有可能需要保存记录,推送"保存"记录后立即按钮。我认为需要一个var"记录",如果他是0则不记录,如果是1是。

保存功能检查是否录制声音并保存在文件夹中。

public class ObjSuperior extends ActionBarActivity {


    int peticion = 1;
    Uri url1;
    int recorded = 0;

    private final String ruta_Sonidos = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Gueopic/";

    private File file = new File(ruta_Sonidos);

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

        //Si no existe crea la carpeta donde se guardaran las fotos

        file.mkdirs();
    }


    public void grabarSo(View v) {
        Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
        startActivityForResult(intent, peticion);
    }

    public void save(){
    //check if file are recorded and save on folder
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == peticion) {
            url1 = data.getData();
        }
    }

1 个答案:

答案 0 :(得分:0)

这适用于我,也许你可以使用它?:

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

final ProgressDialog mProgressDialog = new ProgressDialog(MyActivity.this);
mProgressDialog.setTitle(R.string.lbl_recording);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setButton("Stop recording", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
    mProgressDialog.dismiss();
    recorder.stop();
    recorder.release();
    }
});

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

}