Android当我们点击android中的停止按钮并指定音频文件名时,如何获取弹出窗口

时间:2013-12-03 06:25:07

标签: android audio audio-recording

我的录音应用程序中有2个问题

1)我想要一个弹出窗口,当我按下录音活动的停止按钮时会询问音频名称,我创建了这个活动但它没有打开弹出窗口并询问名称,它用currentTimeMillis存储音频已在代码中指定,那么如何获取要求输入音频名称的弹出窗口,并存储具有特定输入名称的音频。

2)我设置了20秒的计时器,mediaRecoder.setMaxDuration(20000);所以我想要的是在20秒后,我希望录音自动停止并弹出来询问录制的音频名称,与要求1相同,但它应该在20秒后自动停止。

以下是我的活动代码。

public class Record_Audio extends Activity {

private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";

private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4,
        MediaRecorder.OutputFormat.THREE_GPP };
private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4,
        AUDIO_RECORDER_FILE_EXT_3GP };
Chronometer myChronometer;
Handler seekHandler = new Handler();

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

    myChronometer = (Chronometer) findViewById(R.id.chronometer);

    setButtonHandlers();
    enableButtons(false);
    setFormatButtonCaption();
}

private void setButtonHandlers() {
    ((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
    ((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
    ((Button) findViewById(R.id.btnFormat)).setOnClickListener(btnClick);
    ((Button) findViewById(R.id.allrecording)).setOnClickListener(btnClick);
}

private void enableButton(int id, boolean isEnable) {
    ((Button) findViewById(id)).setEnabled(isEnable);
}

private void enableButtons(boolean isRecording) {
    enableButton(R.id.btnStart, !isRecording);
    enableButton(R.id.btnFormat, !isRecording);
    enableButton(R.id.btnStop, isRecording);
}

private void setFormatButtonCaption() {
    ((Button) findViewById(R.id.btnFormat))
            .setText(getString(R.string.audio_format) + " ("
                    + file_exts[currentFormat] + ")");
}

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

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


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();
    }
    myChronometer.setBase(SystemClock.elapsedRealtime());
    myChronometer.start();
}

private void stopRecording() {
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release();

        recorder = null;
        myChronometer.stop();
    }
}

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

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

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

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

private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    @Override
    public void onError(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Record_Audio.this, "Error: " + what + ", " + extra,
                Toast.LENGTH_SHORT).show();
    }
};

private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Record_Audio.this,
                "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
                .show();
    }
};

private View.OnClickListener btnClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnStart: {
            Toast.makeText(Record_Audio.this, "Start Recording",
                    Toast.LENGTH_SHORT).show();
            enableButtons(true);
            startRecording();

            break;
        }
        case R.id.btnStop: {
            Toast.makeText(Record_Audio.this, "Stop Recording",
                    Toast.LENGTH_SHORT).show();
            enableButtons(false);
            stopRecording();

            break;
        }
        case R.id.btnFormat: {
            displayFormatDialog();

            break;
        }

        case R.id.allrecording: {
            Intent intent = new Intent(Record_Audio.this, PlayAudio.class);
            startActivity(intent);
            break;
        }
        }
    }
};
}

所以主要是这个下面的函数负责所有的操作,但我没有得到如何编码。

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

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

任何帮助将不胜感激。提前谢谢......

0 个答案:

没有答案