This article describes the method of developing recording and playing recordings on Android programming. Share it for your reference, as follows:
/* * The application needs to have the permission to write to external storage * if the output file is written to the external storage, and also the * permission to record audio. These permissions must be set in the * application's file, with something like: * * <uses-permission android:name=".WRITE_EXTERNAL_STORAGE" /> * <uses-permission android:name=".RECORD_AUDIO" /> * */ package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class AudioRecordTest extends Activity { private static final String LOG_TAG = "AudioRecordTest"; private static String mFileName = null; private RecordButton mRecordButton = null; private MediaRecorder mRecorder = null; private PlayButton mPlayButton = null; private MediaPlayer mPlayer = null; private void onRecord(boolean start) { if (start) { startRecording(); } else { stopRecording(); } } private void onPlay(boolean start) { if (start) { startPlaying(); } else { stopPlaying(); } } private void startPlaying() { mPlayer = new MediaPlayer(); try { (mFileName); (); (); } catch (IOException e) { (LOG_TAG, "prepare() failed"); } } private void stopPlaying() { (); mPlayer = null; } private void startRecording() { mRecorder = new MediaRecorder(); (); (.THREE_GPP); (mFileName); (.AMR_NB); try { (); } catch (IOException e) { (LOG_TAG, "prepare() failed"); } (); } private void stopRecording() { (); (); mRecorder = null; } class RecordButton extends Button { boolean mStartRecording = true; OnClickListener clicker = new OnClickListener() { public void onClick(View v) { onRecord(mStartRecording); if (mStartRecording) { setText("Stop recording"); } else { setText("Start recording"); } mStartRecording = !mStartRecording; } }; public RecordButton(Context ctx) { super(ctx); setText("Start recording"); setOnClickListener(clicker); } } class PlayButton extends Button { boolean mStartPlaying = true; OnClickListener clicker = new OnClickListener() { public void onClick(View v) { onPlay(mStartPlaying); if (mStartPlaying) { setText("Stop playing"); } else { setText("Start playing"); } mStartPlaying = !mStartPlaying; } }; public PlayButton(Context ctx) { super(ctx); setText("Start playing"); setOnClickListener(clicker); } } public AudioRecordTest() { mFileName = ().getAbsolutePath(); mFileName += "/audiorecordtest.3gp"; } @Override public void onCreate(Bundle icicle) { (icicle); LinearLayout ll = new LinearLayout(this); mRecordButton = new RecordButton(this); (mRecordButton, new ( .WRAP_CONTENT, .WRAP_CONTENT, 0)); mPlayButton = new PlayButton(this); (mPlayButton, new ( .WRAP_CONTENT, .WRAP_CONTENT, 0)); setContentView(ll); } @Override public void onPause() { (); if (mRecorder != null) { (); mRecorder = null; } if (mPlayer != null) { (); mPlayer = null; } } }
For more information about Android related content, please check out the topic of this site:Android multimedia operation skills summary (audio, video, recording, etc.)》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android database operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.