Referring to the online than people write, and then use the time is placed in a new thread to play music, and later found each time after entering the activity will be repeated to start a music playback sound. In order to avoid repeating the playback function I added a singleton mode based on the original code. This avoids repeated playback.
Package Com.liu.zhen.utils;import Android.content.context;import Android.content.res.assetfiledescriptor;import Android.media.mediaplayer;import android.util.log;/** * * This class was used for controlling background music * */public Class Backgroundmusic {private static Backgroundmusic Backgroundmusic = null;private static final String TAG = "Bg_music" ;p rivate float mleftvolume;private float mrightvolume;private Context mcontext;private MediaPlayer Mbackgroundmediaplayer;private boolean mispaused;private String mcurrentpath;private backgroundmusic (Context context ) {This.mcontext = Context;initdata ();} public static Backgroundmusic getinstance (context context) {if (Backgroundmusic = = null) {Backgroundmusic = new Background Music (context);} return backgroundmusic;} Initialize some data private void InitData () {mleftvolume = 0.5f;mrightvolume = 0.5f;mbackgroundmediaplayer = null;mispaused = false; Mcurrentpath = null;} /** * Play background music according to Path PATH * * @param path *: Audio path in assets * @param isloop * : whether to loop */public void Playbackgroundmusic (String path, Boolean isloop) {if (Mcurrentpath = = null) {//This is the first time the background music is played ---it is the first time to play background music//or after the end () method is executed, it is called again---or end () was Calledmbackgroundmediaplayer = creat Emediaplayerfromassets (path); mcurrentpath = path;} else {if (!mcurrentpath.equals path) {//play a new background music---play new background music//release the old resource and generate a new----release older resource and create a new Oneif (mbackgroundmediaplayer! = null) {mbackgroundmediaplayer.release ();} Mbackgroundmediaplayer = createmediaplayerfromassets (path);//Record this path---the record the Pathmcurrentpath = Path;}} if (Mbackgroundmediaplayer = = null) {LOG.E (TAG, "playbackgroundmusic:background Media Player is null");} else {//If music is being broadcast Put or nearly interrupted, stop it---if the music is playing or paused, stop itmbackgroundmediaplayer.stop (); mbackgroundmediaplayer.setlooping (isloop); try {mbackgroundmediaplayer.prepare (); mbackgroundmediaplayer.seekto (0); Mbackgroundmediaplayer.start (); this.mispaused = false;} catch (EXception e) {log.e (TAG, "playbackgroundmusic:error State");}}} /** * Stop playing background music */public void Stopbackgroundmusic () {if (Mbackgroundmediaplayer! = null) {mbackgroundmediaplayer.stop (); /should set the state, if not, the following sequence would be//error//play---stop, Resumethis.mis Paused = false;}} /** * Pause playing background music */public void Pausebackgroundmusic () {if (Mbackgroundmediaplayer! = null&& Mbackgroundmediaplayer . IsPlaying ()) {mbackgroundmediaplayer.pause (); this.mispaused = True;}} /** * Continue playing background music */public void Resumebackgroundmusic () {if (Mbackgroundmediaplayer! = null && this.mispaused) {Mback Groundmediaplayer.start (); this.mispaused = false;}} /** * Replay background music */public void Rewindbackgroundmusic () {if (Mbackgroundmediaplayer! = null) {Mbackgroundmediaplayer.stop () ; try {mbackgroundmediaplayer.prepare (); mbackgroundmediaplayer.seekto (0); Mbackgroundmediaplayer.start (); this.mispaused = false;} catch (Exception e) {log.e (TAG, "Rewindbackgroundmusic:error state");}}} /** * Determines if the background music is playing * * @return: The returned Boolean value represents whether the */public Boolean isbackgroundmusicplaying () {boolean ret = False;if (M) is being played Backgroundmediaplayer = = null) {ret = false;} else {ret = mbackgroundmediaplayer.isplaying ();} return ret;} /** * Ends the background music and frees the resource */public void End () {if (Mbackgroundmediaplayer! = null) {mbackgroundmediaplayer.release ();} Re-"Initialize Data" InitData ();} /** * Get background music "volume" * @return */public float Getbackgroundvolume () {if (This.mbackgroundmediaplayer! = null) {return (thi S.mleftvolume + this.mrightvolume)/2;} else {return 0.0f;}} /** * Set the volume of the background music * * @param volume *: Set playback volume, float type */public void Setbackgroundvolume (float volume) {this.mle Ftvolume = This.mrightvolume = Volume;if (This.mbackgroundmediaplayer! = null) {This.mBackgroundMediaPlayer.setVolume ( This.mleftvolume,this.mrightvolume);}} /** * Create MediaPlayer for Music * * @param path * The path relative to assets * @return */private Mediaplay Er createmediaplayerfromassets (String path) {MedIaplayer MediaPlayer = null;try {Assetfiledescriptor assetfiledescritor = Mcontext.getassets (). OPENFD (path); MediaPlayer = new MediaPlayer (); Mediaplayer.setdatasource (Assetfiledescritor.getfiledescriptor (), Assetfiledescritor.getstartoffset (), assetfiledescritor.getlength ()); Mediaplayer.prepare (); Mediaplayer.setvolume (Mleftvolume, mrightvolume);} catch (Exception e) {mediaPlayer = null; LOG.E (TAG, "error:" + e.getmessage (), e);} return mediaPlayer;}}
Android for background music playback