Network walkie talkie C # server Android client (3) Android client code analysis attachment

Source: Internet
Author: User

Complete Project (source code): http://download.csdn.net/detail/zhujinghao09/5313666

The implementation of the recording function, at the beginning, uses the Android system to bring its own recording class, but its encoding method, PC's windowsmediaplayer does not recognize that it cannot be played, so you can find a non-compressed recording class from the Internet, the format of the recording file is the same as that of windows.

This recording category will not be posted here. If you are interested, you can find it on the EOE forum or download the source code of my project, which contains

Here, we simply use it to generate a non-compressed recording file:

 

 
/*** Recording WAV format file * @ Param path: file path */public static file recordchat (extaudiorecorder, string savepath, string filename) {file dir = new file (savepath); // If the directory does not exist, create the directory if (dir. list () = NULL) {dir. mkdirs ();} // obtain the recording file = new file (savepath + filename); // set the output file extaudiorecorder. setoutputfile (savepath + filename); extaudiorecorder. prepare (); // start the recording extaudiorecorder. start (); Return file;}/** stop recording * @ Param mediarecorder the recorder to be stopped * @ return returns */public static void stoprecord (final extaudiorecorder) {extaudiorecorder. stop (); extaudiorecorder. release ();}

// Recording button

Btnrecod. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view arg0) {int H = new date (). gethours (); int M = new date (). getminutes (); int S = new date (). getseconds (); string strtime = string. valueof (h) + "-" + String. valueof (m) + "-" + String. valueof (s); // obtain the system time name file string savepath = "sdcard/myvoice/"; string filename = "extaudio" + strtime + ". wav "; // todo auto-generated method stubextaudiorecorder = extaudiorecorder. getinstanse (false); // uncompressed recording (WAV) recordchat (extaudiorecorder, savepath, filename); // mrecorder. onstart ("er" + strtime); // lastrecordpath = "sdcard/myvoice/" + "er" + strtime + ". amr "; lastrecordpath = savepath + filename; // global variable }});

Stop recording button

 

 

Btnstop. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view arg0) {// todo auto-generated method stub // mrecorder. onstop (); stoprecord (extaudiorecorder); vpatharraylist. add (lastrecordpath); updatevoicelistview (vpatharraylist); // Add the recording file path to the sound list }});

The playback function is a mediaplayer that comes with the Android system.

 

Playback class:

 

Package er. fly; import Java. io. ioexception; import android. media. mediaplayer; public class mymediaplay {mediaplayer; Public void onplay (string namepath) {mediaplayer = new mediaplayer (); If (mediaplayer. isplaying () {mediaplayer. reset ();} Try {mediaplayer. setdatasource (namepath); mediaplayer. prepare (); mediaplayer. start (); mediaplayer. setoncompletionlistener (New mediaplayer. oncompletionlistener () {@ overridepublic void oncompletion (mediaplayer arg0) {// todo auto-generated method stubmediaplayer. release () ;}});} catch (illegalargumentexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (illegalstateexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} public void onstop () {mediaplayer. stop (); mediaplayer. release (); mediaplayer = NULL ;}}

The playback and sent file paths are read from the sound list.

 

 

Btnplay. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view arg0) {log. I (TAG, "play" + playsendpath); If (playsendpath! = NULL) {mplay. onplay (playsendpath); tvrecvcount. settext ("");} elsetoast. maketext (mainactivity. This, "select the sound to play !! ", Toast. length_long ). show (); playsendpath = NULL ;}}); btnsend. setonclicklistener (New button. onclicklistener () {string Path = NULL; file; // = new file ("sdcard/myrecvvoice/androidrecv1_"); @ overridepublic void onclick (view arg0) {// todo auto-generated method stubif (playsendpath! = NULL) {file = new file (playsendpath);} else {toast. maketext (mainactivity. This, "select the sound to send !! ", Toast. length_long ). show (); return;} playsendpath = NULL; upfile = new upfile (client); upfile. setfile (File); upfile. start ();}});

Where

 
Playsendpath is a global variable that changes its value by selecting a group in the list control.
Public void updatevoicelistview (arraylist <string> Al) {string [] STR = new string [Al. size ()]; for (INT I = 0; I <Al. size (); I ++) {STR [I] = Al. get (I);} arrayadapter <string> AAD = new arrayadapter <string> (this, android. r. layout. simple_list_item_checked, STR); setlistadapter (AAD);} public void onlistitemclick (listview parent, view V, int position, long ID) {checkedtextview item = (checkedtextview) V; string Path = vpatharraylist. get (position); log. I (TAG, PATH); playsendpath = path ;}
 

 
 

 

 
 
 
Finally, the main thread processes messages from sub-threads: The main function is to update the control status of the main interface.

 

 

Package er. fly. nettandclient; import Java. util. arraylist; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. util. log; import android. widget. textview; public class myhandler extends handler {textview tvrecvcount; string tag = "myhandler"; // arraylist <string> voicealist; Public myhandler (Looper loler) {super (Looper );} public void sethandlerargs (textview TV) {tvrecvcount = TV ;}@ override public void handlemessage (Message MSG) {// process the message mainactivity MA = new mainactivity (); string path; string dflag = "downloadfile:"; string strmsg = MSG. OBJ. tostring (); log. I (TAG, strmsg); If (strmsg. indexof (dflag) = 0) {int index1 = strmsg. indexof ("#"); int index2 = strmsg. indexof ("$"); If (index1> 0) {Path = strmsg. substring (dflag. length (), index1); log. I (TAG, PATH); mainactivity. vpatharraylist. add (PATH); log. I (TAG, strmsg. substring (index1 + 1); tvrecvcount. settext (strmsg. substring (index1 + 1);} If (index2> 0) {log. I (TAG, strmsg. substring (index2 + 1); tvrecvcount. settext (strmsg. substring (index1 + 1 ));}}}}
By now, the basic functions of the entire project can be implemented. If you are interested, refer to them.

 

 
Complete Project (source code): http://download.csdn.net/detail/zhujinghao09/5313666

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: info-contact@alibabacloud.com 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.