WeChat-like speech conversation Simple Model

Source: Internet
Author: User

To achieve a similar voice call effect as needed, we can come up with a simple model for your reference:

If you don't talk nonsense, paste the Code directly:

Recoderdemo. Java:

Package com. jankey. record;

Import java. Io. file;
Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. List;
Import Android. App. activity;
Import Android. Graphics. color;
Import Android. Media. mediaplayer;
Import Android. OS. Bundle;
Import Android. OS. environment;
Import Android. View. motionevent;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. View. View. onlongclicklistener;
Import Android. View. View. ontouchlistener;
Import Android. widget. Button;
Import Android. widget. linearlayout;

/**
*
* @ Author jankey
*
*/
Public class recorddemo extends activity {

Private button record;
Private mydialog dialog;
Private audiorecorder Mr;
Private linearlayout linear;
Private mediaplayer;
Private file directory;
Private button BTN = NULL;

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. record_audio );
Mr = new audiorecorder ("jankey ");
Record = (button) This. findviewbyid (R. Id. Record );
Linear = (linearlayout) This. findviewbyid (R. Id. showviews );
Record. setonlongclicklistener (New onlongclicklistener (){
@ Override
Public Boolean onlongclick (view v ){
Dialog = new mydialog (recorddemo. This, "Recording ");
Try {
Record. settext ("recording ...");
Mr. Start ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Dialog. Show ();
Return false;
}
});
Record. setontouchlistener (New ontouchlistener (){
@ Override
Public Boolean ontouch (view V, motionevent event ){
Switch (event. getaction ()){
Case motionevent. action_up:
Try {
Mr. Stop ();
Record. settext ("the recording is stopped! ");
} Catch (ioexception e ){
E. printstacktrace ();
}
Dialog. Dismiss ();
Showview ();
Break;
}
Return false;
}
});
}

Private void showview (){
For (INT I = 0; I <apklist. Size (); I ++ ){
// Num ++;
BTN = new button (this );
BTN. setbackgroundcolor (color. Gray );
BTN. setwidth (200 );
BTN. setheight (50 );
BTN. settext ("click to listen ");
BTN. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Playfile ();
Mediaplayer = new mediaplayer ();
Try {
Mediaplayer. setdatasource (directory. getabsolutepath ());
Mediaplayer. Prepare ();
} Catch (illegalargumentexception e ){
E. printstacktrace ();
} Catch (securityexception e ){
E. printstacktrace ();
} Catch (illegalstateexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Mediaplayer. Start ();
BTN. settext ("playing ");
}
});
Linear. addview (BTN );
}
}

Private void playfile (){
List <string> getfiles = getfiles (
Environment. getexternalstoragedirectory () + "/", ". 3GP", true );
For (string: getfiles ){
System. Out. println (string );
}
}

Private list <string> apklist = new arraylist <string> ();
Public list <string> getfiles (string path, string extension,
Boolean isiterative)
{
File [] files = new file (PATH). listfiles ();
For (INT I = 0; I <files. length; I ++ ){
File F = files [I];
If (F. isfile ()){
If (F. getpath ()
. Substring (F. getpath (). Length ()-extension. Length ())
. Equals (Extension ))
Apklist. Add (F. getpath ());
If (! Isiterative)
Break;
} Else if (F. isdirectory () & F. getpath (). indexof ("/.") =-1)
Getfiles (F. getpath (), extension, isiterative );
}
Return apklist;
}
}

Audiorecorder. Java:

Package com. jankey. record;

Import java. Io. file;
Import java. Io. ioexception;
Import Android. Media. mediarecorder;
Import Android. OS. environment;

/**
*
* @ Author jankey
*
*/
Public class audiorecorder {

Final mediarecorder recorder = new mediarecorder ();
Final string path;

Public audiorecorder (string path ){
This. Path = sanitizepath (PATH );
}

Private string sanitizepath (string path ){
If (! Path. startswith ("/")){
Path = "/" + path;
}
If (! Path. Contains (".")){
Path + = ". 3GP ";
}
Return environment. getexternalstoragedirectory (). getabsolutepath () + path;
}

Public void start () throws ioexception {
String state = Android. OS. environment. getexternalstoragestate ();
If (! State. Equals (Android. OS. environment. media_mounted )){
Throw new ioexception ("SD card is not mounted, it is" + state + ".");
}
File directory = new file (PATH). getparentfile ();
If (! Directory. exists ()&&! Directory. mkdirs ()){
Throw new ioexception ("path to file cocould not be created ");
}
Recorder. setaudiosource (mediarecorder. audiosource. Mic );
Recorder. setoutputformat (mediarecorder. outputformat. three_gpp );
Recorder. setaudioencoder (mediarecorder. audioencoder. amr_nb );
Recorder. setoutputfile (PATH );
Recorder. Prepare ();
Recorder. Start ();
}

Public void stop () throws ioexception {
Recorder. Stop ();
Recorder. Release ();
}
}

Mydialog. Java:

Package com. jankey. record;

Import Android. App. Dialog;
Import Android. content. context;
Import Android. Graphics. color;
Import Android. OS. Bundle;
Import Android. View. window;
Import Android. View. windowmanager;
Import Android. widget. Button;

/**
*
* @ Author jankey
*
*/
Public class mydialog extends dialog {

Private context;
Private string ly;
Private button Lyss;

Public mydialog (context, string Str ){
Super (context );
This. Context = context;
This. Ly = STR;
}

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Requestwindowfeature (window. feature_no_title );
Getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen,
Windowmanager. layoutparams. flag_fullscreen );
Setcontentview (R. layout. my_dialog );
Lyss = (button) This. findviewbyid (R. Id. Lys );
Lyss. settext (ly );
Lyss. setbackgroundcolor (color. Blue );
}

}

Anim: rotate. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Rotate xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Required Tx = "50%" Android: Required ty = "50%"
Android: fromdegrees = "0"
Android: todegrees = "360">
<Shape
Android: Shape = "ring"
Android: innerradiusratio = "3"
Android: thicknessratio = "8"
Android: uselevel = "false">
<Gradient Android: TYPE = "sweep"
Android: uselevel = "false"
Android: startcolor = "#000000"
Android: centercolor = "# ffffff"
Android: centery= "0.50"
Android: endcolor = "# FFFF00"/>
</Shape>
</Rotate>

Background1.png:

Record_audio.xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: Orientation = "vertical"
Android: gravity = "center_horizontal"
>
<Button
Android: Id = "@ + ID/record"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "press and hold to start recording"
/>
<Linearlayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/showviews"
Android: Background = "#556633"
/>
</Linearlayout>

Alphaanimation AA = new alphaanimation (0.1f, 1.0f );
AA. setduration (3000 );
Iv01.startanimation (AA );
AA. setanimationlistener (New animationlistener ()
{

@ Override
Public void onanimationend (animation ){
// Todo auto-generated method stub
Intent it = new intent (logoactivity. This, loginactivity. Class );
Logoactivity. This. startactivity (it );
Finish ();
}

@ Override
Public void onanimationrepeat (animation ){
// Todo auto-generated method stub

}

@ Override
Public void onanimationstart (animation ){
// Todo auto-generated method stub

}

}
);

My_dialog.xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: Orientation = "vertical"
Android: gravity = "center"
>
<Textview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "yicloudong"
Android: textsize = "30dip"
Android: textcolor = "#665533"
/>
<Button
Android: Id = "@ + ID/Lys"
Android: layout_width = "200dip"
Android: layout_height = "50dip"
Android: text = "Recording"
Android: Background = "#884477"
/>
<Progressbar
Android: Id = "@ + ID/loadprogressbar"
Android: Background = "@ drawable/background1"
Android: indeterminatedrawable = "@ anim/rotate"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</Linearlayout>

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.