Android MediaRecorder recording
Recording-related Permissions
Main program code:
Package com. yqy. yqy_record; import java. io. file; import java. io. IOException; import java. text. simpleDateFormat; import java. util. arrayList; import java. util. date; import java. util. hashMap; import java. util. list; import java. util. map; import android. annotation. suppressLint; import android. app. activity; import android. media. mediaPlayer; import android. media. mediaPlayer. onCompletionListener; import android. media. mediaRecorder; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. button; import android. widget. listView; import android. widget. simpleAdapter; public class MainActivity extends Activity {private Button btn; private ListView lv; private SimpleAdapter adapter; private MediaRecorder recorder; private static final String RECORD_DIR = "temp_record "; // obtain the path private File dir = new File (Environment. getExternalStorageDirectory (), RECORD_DIR); private List
> Records = new ArrayList
> (); Private String currnetPath; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btn = (Button) findViewById (R. id. recordBtn); lv = (ListView) findViewById (R. id. recordList); checkDir (); adapter = new SimpleAdapter (this, records, R. layout. list_cell, new String [] {"name"}, new int [] {R. id. cell_ TV}); refreshAdapter (); lv. setAdapter (adapter); lv. setOnItemClickListener (new OnItemClickListener () {@ SuppressWarnings ("unchecked") @ Overridepublic void onItemClick (AdapterView
Arg0, View arg1, int position, long arg3) {Map
Map = (Map
) Adapter. getItem (position); System. out. println (map); final MediaPlayer mp = MediaPlayer. create (MainActivity. this, Uri. fromFile (new File (map. get ("path"); if (mp = null) {return;} mp. start (); mp. setOnCompletionListener (new OnCompletionListener () {@ Overridepublic void onCompletion (MediaPlayer arg0) {mp. release () ;}}) ;}}); btn. setOnTouchListener (new OnTouchListener () {@ Overridepublic boolean onTouch (View v, M OtionEvent) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: recorder = new MediaRecorder (); // The call sequence of these methods cannot be wrong. setAudioSource (MediaRecorder. audioSource. MIC); recorder. setOutputFormat (MediaRecorder. outputFormat. THREE_GPP); recorder. setAudioEncoder (MediaRecorder. audioEncoder. AMR_NB); // obtain the address of the currently stored file currnetPath = getPath (); recorder. setOutputFile (currnetPath); try {recorder. prepare () ;} Catch (IllegalStateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} recorder. start (); break; case MotionEvent. ACTION_UP: if (recorder! = Null) {try {recorder. stop (); refreshAdapter ();} catch (RuntimeException stopException) {// This exception occurs immediately after recording the stopException. printStackTrace (); File file = new File (currnetPath); if (file. exists () {// if an exception is thrown, the error file will be deleted. delete () ;}} finally {recorder. release (); recorder = null ;}} break ;}return false ;}});} private void checkDir () {if (! Dir. exists () {dir. mkdir () ;}/ *** refresh data */private void refreshAdapter () {records. clear (); for (File file: dir. listFiles () {Map
Map = new HashMap
(); Map. put ("name", file. getName (); map. put ("path", file. getAbsolutePath (); records. add (map);} adapter. notifyDataSetChanged () ;}@ SuppressLint ("SimpleDateFormat") private String getPath () {String name = new SimpleDateFormat ("yyyyMMdd_HHmmss_SS "). format (new Date (); File path = new File (dir, name + ". amr "); return path. getAbsolutePath ();}}