Android for background music playback

Source: Internet
Author: User

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

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.