如何在Android应用程序上更改音频格式

时间:2013-01-23 07:58:27

标签: android

其实我正在录音,我想选择我需要的音频文件,我创建弹出框以显示格式选项,但我不能选择选择的一个,在我的情况下它将采用默认或第一格式。 有人可以帮帮我吗?

public String getFilename(){
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath,AUDIO_RECORDER_FOLDER);

    if(!file.exists()){
        file.mkdirs();
    }
    gfilename = (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
    return (gfilename );
}

@SuppressLint("NewApi")
private void startRecording(){
    recorder = new MediaRecorder();         
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(getFilename());          
    //recorder.setOnErrorListener(errorListener);
    //recorder.setOnInfoListener(infoListener);

    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void stopRecording(){
    if(null != recorder)
    {
        mDbHelper.updateTestFileName(mRowId, gfilename); 
        recorder.stop();
        recorder.reset();
        recorder.release();             
        recorder = null;
        }
    displayFormatDialog();

}

private void displayFormatDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    String formats[] = {"MPEG 4", "3GPP", "AMR", "Mp3"};

    builder.setTitle(getString(R.string.choose_format_title))
           .setSingleChoiceItems(formats, currentFormat, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                currentFormat = which;
                //setStopButtonCaption();

                dialog.dismiss();
            }
           })
           .show();
}

0 个答案:

没有答案