Code snippets, code sharing, PHP code sharing, Java code sharing, Ruby code sharing, Python code sharing, HTML code sharing, CSS code sharing, SQL code sharing, and JavaScript code sharing
Package com. example. testaudio; import java. io. file; import android. app. activity; import android. media. mediaPlayer; import android. media. mediaPlayer. onCompletionListener; import android. media. mediaRecorder; import android. OS. bundle; import android. OS. environment; import android. OS. handler; import android. OS. message; import android. util. log; import android. view. menu; import android. view. motionEvent; import Android Oid. view. view; import android. view. view. onClickListener; import android. view. view. onTouchListener; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity {private MediaRecorder recoder = null; private MediaPlayer player = null; private String theMediaPath; TextView TV = null; TextView tvRecord = null; Button testBtn = null; button testBtn2 = null; Ton stopBtn = null; Button playBtn = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TV = (TextView) findViewById (R. id. textView1); tvRecord = (TextView) findViewById (R. id. tvRecord); testBtn = (Button) findViewById (R. id. button1); testBtn2 = (Button) findViewById (R. id. button2); stopBtn = (Button) findViewById (R. id. buttonStop); playBtn = (Button) findViewById (R. id. buttonPlay); testBtn2.setOnTouchListener (new OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {Log. I ("testactivity", "setOnTouchListener:" + event. getAction (); switch (event. getAction () {case MotionEvent. ACTION_UP: {Log. I ("testactivity", "Stop recording"); stopRecording (); break;} case MotionEvent. ACTION_DOWN: {Log. I (" Testactivity "," start recording "); startRecording (); break;} default: break;} return false ;}); testBtn. setOnClickListener (new OnClickListener () {public void onClick (View arg0) {startRecording (); testBtn. setEnabled (false); stopBtn. setEnabled (true) ;}}); stopBtn. setEnabled (false); stopBtn. setOnClickListener (new OnClickListener () {public void onClick (View arg0) {stopRecording (); testBtn. setEnabled (tru E); playBtn. setEnabled (true); stopBtn. setEnabled (false) ;}}); playBtn. setOnClickListener (new OnClickListener () {public void onClick (View arg0) {playRecordFile (theMediaPath); stopBtn. setEnabled (true) ;}}) ;}protected void playRecordFile (String _ file) {try {File f = new File (_ file); if (! F. exists () {TV. setText ("The file does not exist:" + _ file); return ;}} catch (Exception e) {Log. I ("testactivity", e. getMessage ();} try {player = new MediaPlayer (); player. setDataSource (_ file); player. prepare (); player. setOnCompletionListener (new OnCompletionListener () {public void onCompletion (MediaPlayer arg0) {TV. setText ("playback completed"); stopBtn. setEnabled (false) ;}}); player. start ();} catch (Exception e) {Log. e ("Testactivity", "play failed:" + e. getMessage () ;}/ *** stop recording processing */protected void stopRecording () {if (recoder! = Null) {Log. I ("testactivity", "Stop recording"); recoder. stop (); recoder. release (); recoder = null; endtime = System. currentTimeMillis (); _ handleRecordComplete ();} if (player! = Null) {Log. I ("testactivity", "stop playing"); player. stop (); player. release (); player = null ;}/ *** start recording processing */protected void startRecording () {theMediaPath = Environment. getExternalStorageDirectory (). getAbsolutePath (); theMediaPath + = "/audiotest.3gp"; recoder = new MediaRecorder (); recoder. setAudioSource (MediaRecorder. audioSource. MIC); recoder. setOutputFormat (MediaRecorder. outputFormat. THREE_GPP ); Recoder. setOutputFile (theMediaPath); recoder. setAudioEncoder (MediaRecorder. audioEncoder. AMR_NB); starttime = System. currentTimeMillis (); updateMicStatus (); try {recoder. prepare (); recoder. start (); _ handleRecordStart ();} catch (Exception e) {Log. e ("testactivity", "prepared failed:" + e. getMessage (); _ handleRecordStartError (e) ;}// timer private static long maxtime = 30*1000; // 30 seconds private long Starttime = 0l; private long endtime = 0l; private final Handler mHandler = new Handler (); private Runnable mUpdateMicStatusTimer = new Runnable () {public void run () {// Determine whether the request times out if (starttime> 0 & System. currentTimeMillis ()-starttime> maxtime) {Log. e ("testactivity", "time-out recording time, directly stop"); stopRecording (); return ;}// updateMicStatus ();}}; /*** UPDATE The microphone status. the DB is the calculation formula for the relative ring decibel K = 20lg (Vo/Vi) Vo current vibration. The Vi reference value of the amplitude is 600: How do I set the reference value? When 20 ** Math. log10 (mMediaRecorder. when getmaxamplder ()/Vi) = 0, vi is the reference value I need. * when I don't talk to the microphone, I test the obtained mMediaRecorder. the getMaxAmplitude () value is the reference value. * Log. I ("mic _", "microphone reference value:" + mMediaRecorder. getmaxamplstatus (); do not say anything to the microphone if necessary */private int BASE = 600; private int SPACE = 300; // interval sampling time private void updateMicStatus () {if (recoder! = Null) {// int vuSize = 10 * mMediaRecorder. getmaxamplatio ()/32768; int ratio = recoder. getMaxAmplitude ()/BASE; int db = 0; // if (ratio> 1) db = (int) (20 * Math. log10 (ratio); _ handleRecordVoice (db); mHandler. postDelayed (mUpdateMicStatusTimer, SPACE);/** if (db> 1) {vuSize = (int) (20 * Math. log10 (db); Log. I ("mic _", * "microphone volume:" + vuSize);} else Log. I ("mic _", "microphone volume:" + 0); */} private void _ handleRecordStart () {// function TV for receiving the recording. setText ("start recording... "); // starttime start time} private void _ handleRecordStartError (Exception e) {// The receiving function for starting the Recording Fails. TV. setText ("failed to start recording:" + e. getMessage ();} private void _ handleRecordComplete () {// end the recording TV. setText ("stop recording:" + theMediaPath);} private void _ handleRecordVoice (int _ db) {// listen on sound events, convert component: Bay tvRecord. setText ("" + _ db) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the Menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
The above is the detailed content of the recording processing code instance implementing the similar speech function. For more information, see other related articles on php Chinese network!