Android MediaRecorder recording

Source: Internet
Author: User

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 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 ();}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.