开发者

sample project for audio recording in android [duplicate]

This question already has answers here: Closed 10 years ago.

Possible Duplicate:

Audio Recording

Hi im a new developer to android field,i wrote the program to audio recordig in android,bt still now its not working, i wan开发者_运维知识库t to record the human voice for my small application so please give the sample project for develope my application


Try with the Following code example to record Audio

public class AudioRecorder extends Activity {

    private ImageButton button;
    private ImageButton stopbutton;
    private MediaRecorder mediarecorder;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audiorecorderlayout);

        button = (ImageButton)findViewById(R.id.record);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                record("/sdcard/audiorecordexample.3gpp");
            }
        });

        stopbutton = (ImageButton)findViewById(R.id.stop);
        stopbutton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                stopRecord();
            }
        });
    }

    private void record(String filePath) {
        try {

            File mediafile = new File(filePath);

            if (mediafile.exists()) {
                mediafile.delete();
            }
            mediafile = null;

            // record button goes away
            button.setVisibility(View.GONE);

            // stop button shows up
            stopbutton.setVisibility(View.VISIBLE);

            // set up media recorder
            if (mediarecorder == null)
                mediarecorder = new MediaRecorder();
            mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mediarecorder.setOutputFile(filePath)

            // prepare media recorder
            mediarecorder.prepare();

            // start media recorder
            mediarecorder.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void stopRecord() {

        // stop media recorder
        mediarecorder.stop();

        // reset media recorder
        mediarecorder.reset();

        // record button shows up
        button.setVisibility(View.VISIBLE);

        // stop button goes away
        stopbutton.setVisibility(View.GONE);

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜