Android audio recording (MediaRecorder) and playback (MediaPlayer)
You can use MediaRecorder and MediaPlayer to record and play sound. The code is simple and you can directly paste the code. There are only four buttons in the xml file.
UI
Code:
VoiceActivity. class
Package com. zy. ione; import com. zy. media. UPlayer; import com. zy. media. URecorder; import android. app. activity; import android. OS. bundle; import android. OS. environment; import android. view. view; import android. view. view. onClickListener; import android. widget. toast; public class VoiceActivity extends Activity {private String path = null; private URecorder recorder; private UPlayer player; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); this. setContentView (R. layout. activity_voice); path = Environment. getExternalStorageDirectory (). getAbsolutePath (); path + = "/ione. pcm "; recorder = new URecorder (path); player = new UPlayer (path); // start recording findViewById (R. id. ione_record_start_btn ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {Toast. makeText (VoiceActivity. this, "start record", Toast. LENGTH_SHORT ). show (); recorder. start () ;}}); // stop the recording findViewById (R. id. ione_record_stop_btn ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {Toast. makeText (VoiceActivity. this, "stop record", Toast. LENGTH_SHORT ). show (); recorder. stop () ;}}); // start playing findViewById (R. id. ione_play_start_btn ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {Toast. makeText (VoiceActivity. this, "start play", Toast. LENGTH_SHORT ). show (); player. start () ;}}); // stop playing findViewById (R. id. ione_play_stop_btn ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {Toast. makeText (VoiceActivity. this, "stop play", Toast. LENGTH_SHORT ). show (); player. stop ();}});}}Operation Interface
package com.zy.media;public interface IVoiceManager {public boolean start();public boolean stop();}
Recording
Package com. zy. media; import java. io. IOException; import android. media. mediaRecorder; import android. util. log; public class URecorder implements IVoiceManager {private final String TAG = URecorder. class. getName (); private String path; private MediaRecorder mRecorder; public URecorder (String path) {this. path = path; mRecorder = new MediaRecorder ();}/** start recording * @ return boolean */@ Overridepublic boolean start () {// set the audio source to Micphone mRecorder. setAudioSource (MediaRecorder. audioSource. MIC); // set the Encapsulation Format mRecorder. setOutputFormat (MediaRecorder. outputFormat. THREE_GPP); mRecorder. setOutputFile (path); // sets the encoding format mRecorder. setAudioEncoder (MediaRecorder. audioEncoder. AMR_NB); try {mRecorder. prepare ();} catch (IOException e) {Log. e (TAG, "prepare () failed");} // recording mRecorder. start (); return false;}/** stop recording * @ return boolean */@ Overridepublic boolean stop () {mRecorder. stop (); mRecorder. release (); mRecorder = null; return false ;}}
Play
Package com. zy. media; import android. media. mediaPlayer; import android. util. log; public class UPlayer implements IVoiceManager {private final String TAG = UPlayer. class. getName (); private String path; private MediaPlayer mPlayer; public UPlayer (String path) {this. path = path; mPlayer = new MediaPlayer () ;}@ Overridepublic boolean start () {try {// set the file mPlayer to be played. setDataSource (path); mPlayer. prepare (); // play the mPlayer. start ();} catch (Exception e) {Log. e (TAG, "prepare () failed");} return false ;}@ Overridepublic boolean stop () {mPlayer. stop (); mPlayer. release (); mPlayer = null; return false ;}}
How can I enable recording and playing in android software development? If I play a sound, I can hear it on the simulator? Hey, everybody
1. MediaRecorder recording and MediaPlayer playback. Pay attention to their lifecycles during use.
2. There is no problem in playing the sound on the simulator. It can be heard from outside the pc.
You can also ask questions.
Does the android MediaPlayer class get the playback file bit rate?
I don't know if MediaPlayer can be obtained, but the android system can help you resolve it.
Method: In the directory data/com. anroid. contentproviders. there is a database in the media package, which is about a variety of multimedia, and there is a table with attributes related to all the music files (android can recognize other formats), such as Song names, singers, the path and time of the song. Of course, there is also a bit rate. You can use this. You can use contentProvider to query this table. The table Url is MediaStore. Audio. Media. EXTERNAL_CONTENT_URI. You can check it online.
Cursor cursor = getContentResolver (). query (
MediaStore. Audio. Media. EXTERNAL_CONTENT_URI, null,
"Duration> 60000", null, MediaStore. Audio. Media. TITLE );