Playing background music in android

Source: Internet
Author: User

Playing background music in android
After referring to what was written on the internet, I used it to play music in a new thread. Later I found that every time I entered the Activity, I would start playing a music again. To avoid repeated playback, I added the 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 is used for controlling background music **/public class BackgroundMusic {private static BackgroundMusic backgroundMusic = null; private static final String TAG = "Bg_Music "; private float mLeftVolume; private float mRight Volume; 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 BackgroundMusic (context);} return backgroundMusic ;} // initialize some data private void initData () {MLeftVolume = 0.5f; mRightVolume = 0.5f; mBackgroundMediaPlayer = null; mIsPaused = false; mCurrentPath = null ;} /*** play background music based on the path ** @ param path *: the audio path * @ param isLoop * in assets: whether to play cyclically */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, called again --- or end () was calledmBackgroundM EdiaPlayer = createMediaplayerFromAssets (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 old resource and create a new oneif (mBackgroundMediaPlayer! = Null) {mBackgroundMediaPlayer. release ();} mBackgroundMediaPlayer = createMediaplayerFromAssets (path); // record this path --- record the pathmCurrentPath = path;} if (mBackgroundMediaPlayer = null) {Log. e (TAG, "playBackgroundMusic: background media player is null");} else {// if the music is playing or has been interrupted, stop it --- if the music is playing or paused, stop itmBackgroundMediaPlayer. stop (); mBackgroundMediaPlayer. setLooping (isLoop); t Ry {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 (); // shocould set the state, if not, the following sequence will be // error // play-> pause-> stop-> resumethis. mIsPaused = false ;}}/*** pause playing background music */public void pauseBackgroundMusic () {if (mBackgroundMediaPlayer! = Null & mBackgroundMediaPlayer. isPlaying () {mBackgroundMediaPlayer. pause (); this. mIsPaused = true ;}}/*** resume playing background music */public void resumeBackgroundMusic () {if (mBackgroundMediaPlayer! = Null & this. mIsPaused) {mBackgroundMediaPlayer. 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 whether the background music is playing ** @ return: the returned boolean value indicates whether the video is being played */public boolean isBackgroundMusicPlaying () {boolean ret = false; if (mBackgroundMediaPlayer = null) {ret = fal Se;} else {ret = mBackgroundMediaPlayer. isPlaying ();} return ret;}/*** ends the background music and releases the resource */public void end () {if (mBackgroundMediaPlayer! = Null) {mBackgroundMediaPlayer. release ();} // re-"initialize data" initData ();}/*** get the "volume" of the background music ** @ return */public float getBackgroundVolume () {if (this. mBackgroundMediaPlayer! = Null) {return (this. mLeftVolume + this. mRightVolume)/2;} else {return 0.0f;}/*** set the background music volume ** @ param volume *: Set the playback volume, float Type */public void setBackgroundVolume (float volume) {this. mLeftVolume = 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 MediaPlayer 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 ;}}

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.