Play Audio Soundhelper

Source: Internet
Author: User

A tool class for audio
ImportJava.io.File;ImportJava.util.HashMap;ImportAndroid.content.Context;ImportAndroid.media.AudioManager;ImportAndroid.media.MediaPlayer;ImportAndroid.media.SoundPool;ImportAndroid.text.TextUtils; Public  class soundhelper {    Private StaticSoundhelper util;PrivateSoundpool sp;PrivateContext context = app.getinstance ();PrivateHashmap<string, integer> Spmap;PrivateMediaPlayer MediaPlayer;synchronized  Public StaticSoundhelpergetinstance() {if(Util = =NULL) {util =NewSoundhelper (); }returnUtil }Private Soundhelper() {Super(); Spmap =NewHashmap<string, integer> (); SP =NewSoundpool (5, Audiomanager.stream_music,0); MediaPlayer =NewMediaPlayer (); }/** * Play audio under the Raw folder * * @param rawid * @param cyclenum */     Public void PlaySound(intRawid,intCyclenum) {spmap.put (Rawid +"", Sp.load (context, Rawid,1)); PLAYSP (Rawid +"", Cyclenum); }/** * Play external audio * * @param URL * @param cyclenum */     Public void PlaySound(String URL,intCyclenum) {spmap.put (URL, sp.load (URL,1));    PLAYSP (URL, cyclenum); }/** * Play audio via Soundpool * * @param URL * @param cyclenum */    Private void PLAYSP(String URL,intCyclenum) {Audiomanager am = (audiomanager) context. Getsystemservice (Context.audio_service);floatAudiomaxvolumn = Am.getstreammaxvolume (Audiomanager.stream_music);floatVolumncurrent = Am.getstreamvolume (Audiomanager.stream_music);floatVolumnratio = Volumncurrent/audiomaxvolumn; Sp.play (Spmap.get (URL), Volumnratio, Volumnratio,1, Cyclenum,1f); }/** * Pause * * @param Key * *     Public void Pausesp(String key)    {Sp.pause (Spmap.get (key)); }/** * Stop * * @param Key * *     Public void STOPSP(String key)    {Sp.stop (Spmap.get (key)); }/** * Play local or network audio * * @param urlpath * @param isearpiece * Whether to play with the headset * @param seek * Initial progress */     Public void PlayMedia(String URLPath,BooleanIsearpiece,intSeek) {intStreamtype = Audiomanager.stream_music;if(Textutils.isempty (urlpath) | |!NewFile (URLPath). Exists ()) {return; }if(isearpiece)        {streamtype = Audiomanager.stream_voice_call; }if(MediaPlayer = =NULL) {MediaPlayer =NewMediaPlayer (); }Try{Mediaplayer.reset ();            Mediaplayer.setaudiostreamtype (Streamtype);            Mediaplayer.setdatasource (URLPath); Mediaplayer.prepare ();if(Seek >0) {Mediaplayer.seekto (seek);        } mediaplayer.start (); }Catch(Exception e)        {E.printstacktrace (); }    }/** * Start or Resume */     Public void Startmedia() {Mediaplayer.start (); }/** * Get MediaPlayer * * @return  */     PublicMediaPlayerGetmediaplayer() {returnMediaPlayer; }/** * Stop * * @return  */     Public Boolean Stopmedia() {Booleanresult =false;Try{mediaplayer.stop ();            Mediaplayer.release (); result =true; }Catch(IllegalStateException e)            {E.printstacktrace (); result =false; }returnResult }/** * Pause * * @return  */     Public Boolean Pausemedia() {Booleanresult =false;Try{if(Mediaplayer.isplaying ())            {Mediaplayer.pause (); } result =true; }Catch(Exception e)            {E.printstacktrace (); result =false; }returnResult }}

Play Audio Soundhelper

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.