In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use Android to imitate Wechat to record voice. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
The details are as follows
Preface
I divided the recording into two parts.
1.UI interface, pop-up window read seconds 2. A class (including start, stop, create file name functions)
The first part
Due to the 6.0 permission problem, click the button to apply for permission to pass the pop-up window, how to apply for permission
Pop-up window layout popw_record.xml
Bounce
/ * start recording * / private void showPopup () {final View contentView = LayoutInflater.from (Orderdeatil.this) .propagate (R.layout.popw_record, null); mPopWindow = new PopupWindow (contentView, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true); mPopWindow.setContentView (contentView); TextView startRe = (TextView) contentView.findViewById (R.id.startRecord) StartRe.setOnTouchListener (new View.OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {switch (event.getAction ()) {case MotionEvent.ACTION_UP:// loosens the area where the code is executed after the event if (mPopWindow! = null) {mPopWindow.dismiss (); sr.stopRecording ();} break; case MotionEvent.ACTION_DOWN:// holds down the area where the code is executed after the event Chronometer timer = (Chronometer) contentView.findViewById (R.id.timer) Timer.setBase (SystemClock.elapsedRealtime ()); / / timer zeroing timer.start (); / / prompt to start recording sr.startRecording (); break; case MotionEvent.ACTION_CANCEL: if (mPopWindow! = null) {mPopWindow.dismiss (); sr.stopRecording (); / / stop recording} break; default: break;} return true;}}); ImageView close = (ImageView) contentView.findViewById (R.id.close) Close.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {mPopWindow.dismiss ();}}); mPopWindow.setTouchable (true); mPopWindow.setFocusable (true); mPopWindow.setBackgroundDrawable (new BitmapDrawable ()); mPopWindow.setOutsideTouchable (true); mPopWindow.setTouchInterceptor (new View.OnTouchListener () {public boolean onTouch (View v, MotionEvent event) {if (event.getAction () = = MotionEvent.ACTION_OUTSIDE) {mPopWindow.dismiss (); return true;} return false;}) View rootview = LayoutInflater.from (Orderdeatil.this). Evaluate (R.layout.activity_orderdeatil, null); mPopWindow.showAtLocation (rootview, Gravity.CENTER, 0,0);}
Part II tool class
Class SoundRecorder {public void startRecording () {mRecorder = new MediaRecorder (); mRecorder.setAudioSource (MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat (MediaRecorder.OutputFormat.DEFAULT); mRecorder.setAudioEncoder (MediaRecorder.AudioEncoder.DEFAULT); mRecorder.setOutputFile (newFileName ()); try {/ / ready to start recording mRecorder.prepare (); mRecorder.start ();} catch (IllegalStateException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace () }} public void stopRecording () {if (mRecorder! = null) {/ / added by ouyang start try {/ / the following three parameters must be added, otherwise it will fail in mediarecorder.stop (); / / error: RuntimeException:stop failed mRecorder.setOnErrorListener (null); mRecorder.setOnInfoListener (null); mRecorder.setPreviewDisplay (null); mRecorder.stop ();} catch (IllegalStateException e) {/ / TODO: handle exception Log.i ("Exception", Log.getStackTraceString (e)) } catch (RuntimeException e) {/ / TODO: handle exception Log.i ("Exception", Log.getStackTraceString (e));} catch (Exception e) {/ / TODO: handle exception Log.i ("Exception", Log.getStackTraceString (e));} / / added by ouyang end mRecorder.release (); mRecorder = null; upRecord ();}} public String newFileName () {mFileName = Environment.getExternalStorageDirectory () .getAbsolutePath () String s = new SimpleDateFormat ("yyyy-MM-dd hhmmss") .format (new Date ()); return mFileName + = "/ rcd_" + s + ".mp3";}}
This is selected from my code, plus permissions should be possible.
This is the end of the article on "how to use Android to imitate Wechat recording voice function". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.