Before, wrote a blog about the recording of audio on Android, there are some problems are not resolved, here, do an optimization
The previous blog,
http://blog.csdn.net/u014620028/article/details/51283261
optimize the place : There are multiple audio files, click A, play, a during playback, if you click A, stop playing; If you click B,a Stop, play B. Similar to the voice playback of QQ
Now the feature optimization is done on a previous basis, so here only write-optimized places
1. Engineering Catalogue Structure
Added this playback tool class.
Source:
PackageCom.chen.voicedemo;Importandroid.graphics.drawable.AnimationDrawable;ImportAndroid.media.MediaPlayer;ImportAndroid.widget.ImageView;ImportJava.io.File;ImportJava.io.FileInputStream;/** * Voice Playback class * / Public class mediaplayerutils { Private StaticMediaplayerutils mediaplayerutils;PrivateMediaPlayer MediaPlayer;Private Booleanflag=true;Private StaticString Media_path;Private StaticImageView Media_imageview; Public StaticMediaplayerutilsGetinstense(){if(mediaplayerutils==NULL) {mediaplayerutils=NewMediaplayerutils (); }returnMediaplayerutils; } Public void Setplayorstop(String path, ImageView ImageView) {if(Settfvoice (path)) {Media_path=path; Media_imageview=imageview; animationdrawable animation =NULL;Try{animation = (animationdrawable) media_imageview.getdrawable ();if(Animation! =NULL) {if(Animation.isrunning ()) {flag =false;return; }Else{flag =true; } } }Catch(Exception e) {E.printstacktrace (); }Try{if(flag) {flag =false; MediaPlayer =NewMediaPlayer (); Mediaplayer.setonpreparedlistener (NewMediaplayer.onpreparedlistener () {@Override Public void onprepared(MediaPlayer MP) {if(mp!=NULL) {Mp.start (); Media_imageview.setimageresource (R.drawable.voice_anim); Animationdrawable animationdrawable= (animationdrawable) media_imageview.getdrawable (); Animationdrawable.start (); } } }); Mediaplayer.setoncompletionlistener (NewMediaplayer.oncompletionlistener () {@Override Public void oncompletion(MediaPlayer MP) {flag =true;if(Mp.isplaying ()) {mp.release ();//Release Resources}Try{animationdrawable animation = (animationdrawable) media_imageview.getdrawable ();if(Animation! =NULL&& animation.isrunning ()) {animation.stop (); } media_imageview.setimageresource (r.drawable.lcs_voice_receive); }Catch(Exception e) {e.tostring (); } } });if(Path.indexof ("http") == -1&& Path.indexof ("File") == -1) {File File =NewFile (path); FileInputStream FIS =NewFileInputStream (file); Mediaplayer.setdatasource (FIS.GETFD ());//Mediaplayer.setdatasource (Filehelper.getrealfilepath (Bean.getvoice_url ()));}Else{Mediaplayer.setdatasource (path); } mediaplayer.prepare ();//Buffer} }Catch(Exception e) {flag =true; E.printstacktrace (); } } } Public Boolean Setvoice_stop(){if(MediaPlayer = =NULL){return true; }Else if(MediaPlayer! =NULL&&mediaplayer.isplaying ()) {flag=true; Mediaplayer.stop ();Try{animationdrawable animation = (animationdrawable) media_imageview.getdrawable ();if(Animation! =NULL&& animation.isrunning ()) {animation.stop (); } media_imageview.setimageresource (r.drawable.lcs_voice_receive); }Catch(Exception e) {e.tostring (); }return false; }Else if(MediaPlayer! =NULL&&!mediaplayer.isplaying ()) {return true; } mediaplayer=NULL;return false; }Private Boolean Settfvoice(String Path) {BooleanMm=setvoice_stop ();if(mm) {return true; }Else{if(Media_path.equals (path)) {return false; }Else{return true; } } }}
Before the playback of the voice method, is in the ListView Onitemclick method, click that, do the corresponding operation, missing some judgment condition. Here, the package mediaplayerutils only need 2 parameters, you can, in the Onitemclick method
MediaPlayerUtils.getInstense().setPlayorStop(voiceList.get(position), voice_anim);
You can do it.
parameter Description : The first parameter, which is the path of the voice, can be a network path, or it can make a local path. The second parameter is ImageView. That is the picture used to show the sound waves.
Another point to note is that when initializing the display of sonic images, use Setimageresource, do not write background in the XML file, otherwise, it will sometimes cause animation can not move. At the beginning of the initialization, with the static picture, when the need to move, write the XML animation file. For details, see source code
Android recording sound, and playback, feature optimization