In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use Android imitation Wechat voice intercom recording function, the article is very detailed, has a certain reference value, interested friends must finish reading!
The idea of realization is:
1. In Wechat, we can see that voice intercom is achieved by clicking a button, so here I choose to renew my control to inherit from Button and rewrite the onTouchEvent method to judge the recording.
two。 In the onTouchEvent method
When we press the button, we first display the dialog box for recording, then call the recording preparation method and start recording, then start a timing thread to get the volume of the recording every 0.1 seconds, and update the display picture in Dialog based on the volume through Handler.
When we move our finger, if the upward distance of the finger is greater than 50, the prompt to release the finger to cancel the recording is displayed in Dialog, and the isCanceled variable (indicating whether we finally canceled the recording) is set to true, and the moving distance is less than 20:00, we restore the picture of Dialog and set isCanceled to false. When raising the finger, we first close the recording dialog box, then call the recording stop method and close the timing thread, and then we determine whether to cancel the recording, if so, delete the recording file, otherwise determine whether the timing time is too short, and finally call the recordEnd method in the callback API.
3. Here, in order to adapt to different recording needs, I use the strategy mode to deal with each different recording method as a different strategy, according to their own needs to rewrite.
Pay attention to the problem
1. True should be returned in the return value of the onTouchEvent so that other touch events can be shielded, otherwise the finger will not respond to our touch method after sliding away from the Button. two。 Don't forget to add permissions for your App:
Code reference
The RecordButton class, our custom control, overrides the onTouchEvent method
Package com.example.recordtest;import android.annotation.SuppressLint;import android.app.Dialog;import android.content.Context;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.view.Gravity;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class RecordButton extends Button {private static final int MIN_RECORD_TIME = 1 / / minimum recording time (in seconds) private static final int RECORD_OFF = 0; / / not recording private static final int RECORD_ON = 1; / / recording private Dialog mRecordDialog; private RecordStrategy mAudioRecorder; private Thread mRecordThread; private RecordListener listener; private int recordState = 0; / recording status private float recodeTime = 0.0f; / / recording duration. If recording time is too short, recording fails private double voiceValue = 0.0 / / Volume value of recording private boolean isCanceled = false; / / whether to cancel recording private float downY; private TextView dialogTextView; private ImageView dialogImg; private Context mContext; public RecordButton (Context context) {super (context); / / TODO Auto-generated constructor stub init (context);} public RecordButton (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); / / TODO Auto-generated constructor stub init (context);} public RecordButton (Context context, AttributeSet attrs) {super (context, attrs) / / TODO Auto-generated constructor stub init (context);} private void init (Context context) {mContext = context; this.setText ("hold it down");} public void setAudioRecord (RecordStrategy record) {this.mAudioRecorder = record;} public void setRecordListener (RecordListener listener) {this.listener = listener;} / / Dialog private void showVoiceDialog (int flag) {if (mRecordDialog = = null) {mRecordDialog = new Dialog (mContext, R.style.Dialogstyle); mRecordDialog.setContentView (R.layout.dialog_record) DialogImg = (ImageView) mRecordDialog .findViewById (R.id.record_dialog_img); dialogTextView = (TextView) mRecordDialog .findViewById (R.id.record_dialog_txt);} switch (flag) {case 1: dialogImg.setImageResource (R.drawable.record_cancel); dialogTextView.setText ("release your finger to cancel recording"); this.setText ("release your finger to cancel recording"); break; default: dialogImg.setImageResource (R.drawable.record_animate_01) DialogTextView.setText ("Slide up to cancel recording"); this.setText ("release fingers to finish recording"); break;} dialogTextView.setTextSize (14); mRecordDialog.show ();} / / Toast displays private void showWarnToast (String toastText) {Toast toast = new Toast (mContext) when recording time is too short; View warnView = LayoutInflater.from (mContext). Inflate (R.layout.toast_warn, null); toast.setView (warnView); toast.setGravity (Gravity.CENTER, 0,0) / / starting position is intermediate toast.show ();} / / start recording timing thread private void callRecordTimeThread () {mRecordThread = new Thread (recordThread); mRecordThread.start ();} / / recording Dialog pictures switch private void setDialogImage () {if (voiceValue) with recording volume
< 600.0) { dialogImg.setImageResource(R.drawable.record_animate_01); } else if (voiceValue >600.0 & & voiceValue
< 1000.0) { dialogImg.setImageResource(R.drawable.record_animate_02); } else if (voiceValue >1000.0 & & voiceValue
< 1200.0) { dialogImg.setImageResource(R.drawable.record_animate_03); } else if (voiceValue >1200.0 & & voiceValue
< 1400.0) { dialogImg.setImageResource(R.drawable.record_animate_04); } else if (voiceValue >1400.0 & & voiceValue
< 1600.0) { dialogImg.setImageResource(R.drawable.record_animate_05); } else if (voiceValue >1600.0 & & voiceValue
< 1800.0) { dialogImg.setImageResource(R.drawable.record_animate_06); } else if (voiceValue >1800.0 & & voiceValue
< 2000.0) { dialogImg.setImageResource(R.drawable.record_animate_07); } else if (voiceValue >2000.0 & & voiceValue
< 3000.0) { dialogImg.setImageResource(R.drawable.record_animate_08); } else if (voiceValue >3000.0 & & voiceValue
< 4000.0) { dialogImg.setImageResource(R.drawable.record_animate_09); } else if (voiceValue >4000.0 & & voiceValue
< 6000.0) { dialogImg.setImageResource(R.drawable.record_animate_10); } else if (voiceValue >6000.0 & & voiceValue
< 8000.0) { dialogImg.setImageResource(R.drawable.record_animate_11); } else if (voiceValue >8000.0 & & voiceValue
< 10000.0) { dialogImg.setImageResource(R.drawable.record_animate_12); } else if (voiceValue >10000.0 & & voiceValue
< 12000.0) { dialogImg.setImageResource(R.drawable.record_animate_13); } else if (voiceValue >12000.0) {dialogImg.setImageResource (try);} / / recording thread private Runnable recordThread = new Runnable () {@ Override public void run () {recodeTime = 0.0f; while (recordState = = RECORD_ON) {{try {Thread.sleep (100); recodeTime + = 0.1; / / get volume, update dialog if (! isCanceled) {voiceValue = mAudioRecorder.getAmplitude (); recordHandler.sendEmptyMessage (1) } catch (InterruptedException e) {e.printStackTrace ();}; @ SuppressLint ("HandlerLeak") private Handler recordHandler = new Handler () {@ Override public void handleMessage (Message msg) {setDialogImage ();}}; @ Override public boolean onTouchEvent (MotionEvent event) {/ / TODO Auto-generated method stub switch (event.getAction ()) {case MotionEvent.ACTION_DOWN: / / press the button if (recordState! = RECORD_ON) {showVoiceDialog (0) DownY = event.getY (); if (mAudioRecorder! = null) {mAudioRecorder.ready (); recordState = RECORD_ON; mAudioRecorder.start (); callRecordTimeThread ();}} break; case MotionEvent.ACTION_MOVE: / / sliding finger float moveY = event.getY (); if (downY-moveY > 50) {isCanceled = true; showVoiceDialog (1);} if (downY-moveY < 20) {isCanceled = false; showVoiceDialog (0);} break Case MotionEvent.ACTION_UP: / / release finger if (recordState = = RECORD_ON) {recordState = RECORD_OFF; if (mRecordDialog.isShowing ()) {mRecordDialog.dismiss ();} mAudioRecorder.stop (); mRecordThread.interrupt (); voiceValue = 0.0; if (isCanceled) {mAudioRecorder.deleteOldFile ();} else {if (recodeTime < MIN_RECORD_TIME) {showWarnToast ("recording failed too short"); mAudioRecorder.deleteOldFile () } else {if (listener! = null) {listener.recordEnd (mAudioRecorder.getFilePath ());} isCanceled = false; this.setText ("hold down and speak");} break;} return true;} public interface RecordListener {public void recordEnd (String filePath);}}
Dialog layout:
Toast layout for which recording time is too short:
Custom Dialogstyle, dialog box style
@ android:color/transparent @ null true true true @ android:style/Animation.Dialog false
RecordStrategy recording Policy Interface
The package com.example.recordtest;/** * RecordStrategy recording policy interface * @ author acer * / public interface RecordStrategy {/ * is here to prepare the recording, reset the recording file name, etc. / / public void ready (); / * start recording * / public void start (); / * * end of recording * / public void stop () / * delete the old file * / public void deleteOldFile () when recording fails; / * get the recording volume * @ return * / public double getAmplitude (); / * return the full path to the recording file * @ return * / public String getFilePath ();}
A practical strategy of recording written by individuals
Package com.example.recordtest;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import android.media.MediaRecorder;import android.os.Environment;public class AudioRecorder implements RecordStrategy {private MediaRecorder recorder; private String fileName; private String fileFolder = Environment.getExternalStorageDirectory () .getPath () + "/ TestRecord"; private boolean isRecording = false; @ Override public void ready () {/ / TODO Auto-generated method stub File file = new File (fileFolder) If (! file.exists ()) {file.mkdir ();} fileName = getCurrentDate (); recorder = new MediaRecorder (); recorder.setOutputFile (fileFolder + "/" + fileName + ".amr"); recorder.setAudioSource (MediaRecorder.AudioSource.MIC); / / set MediaRecorder audio source to microphone recorder.setOutputFormat (MediaRecorder.OutputFormat.RAW_AMR); / / set MediaRecorder recording audio format recorder.setAudioEncoder (MediaRecorder.AudioEncoder.AMR_NB) / / set the encoding of MediaRecorder recorded audio to amr} / / use the current time as the file name private String getCurrentDate () {SimpleDateFormat formatter = new SimpleDateFormat ("yyyy_MM_dd_HHmmss"); Date curDate = new Date (System.currentTimeMillis ()); / / get the current time String str = formatter.format (curDate); return str;} @ Override public void start () {/ / TODO Auto-generated method stub if (! isRecording) {try {recorder.prepare (); recorder.start () } catch (IllegalStateException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} isRecording = true;}} @ Override public void stop () {/ / TODO Auto-generated method stub if (isRecording) {recorder.stop (); recorder.release (); isRecording = false } @ Override public void deleteOldFile () {/ / TODO Auto-generated method stub File file = new File (fileFolder + "/" + fileName + ".amr"); file.deleteOnExit ();} @ Override public double getAmplitude () {/ / TODO Auto-generated method stub if (! isRecording) {return 0;} return recorder.getMaxAmplitude ();} @ Override public String getFilePath () {/ / TODO Auto-generated method stub return fileFolder + "/" + fileName + ".amr";}}
MainActivity
Package com.example.recordtest;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {RecordButton button; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); button = (RecordButton) findViewById (R.id.btn_record); button.setAudioRecord (new AudioRecorder ());} @ 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 all the contents of the article "how to use Android imitation Wechat voice intercom recording function". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.