背景音乐功能

<strong><span style="color:#ff0000;">package com.yuan.xue.utils;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.util.Log;


public class BackgroundMusic {
	private static BackgroundMusic backgroundMusic = null;
	private static final String TAG = "Music";
	private 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 BackgroundMusic(context);
		}
		return backgroundMusic;
	}

	// 初始化数据
	private void initData() {
		mLeftVolume = 0.5f;
		mRightVolume = 0.5f;
		mBackgroundMediaPlayer = null;
		mIsPaused = false;
		mCurrentPath = null;
	}

	public void playBackgroundMusic(String path, boolean isLoop) {
		if (mCurrentPath == null) {
			
			mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
			mCurrentPath = path;
		} else {
			if (!mCurrentPath.equals(path)) {
				
				if (mBackgroundMediaPlayer != null) {
					mBackgroundMediaPlayer.release();
				}
				mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
				
				mCurrentPath = path;
			}
		}

		if (mBackgroundMediaPlayer == null) {
			Log.e(TAG, "playBackgroundMusic: background media player is null");
		} else {
			
			mBackgroundMediaPlayer.stop();
			mBackgroundMediaPlayer.setLooping(isLoop);
			try {
				mBackgroundMediaPlayer.prepare();
				mBackgroundMediaPlayer.seekTo(0);
				mBackgroundMediaPlayer.start();
				this.mIsPaused = false;
			} catch (Exception e) {
				Log.e(TAG, "playBackgroundMusic: error state");
			}
		}
	}

	/**
	 * 停止播放
	 */
	public void stopBackgroundMusic() {
		if (mBackgroundMediaPlayer != null) {
			mBackgroundMediaPlayer.stop();
			// should set the state, if not , the following sequence will be
			// error
			// play -> pause -> stop -> resume
			this.mIsPaused = false;
		}
	}

	/**
	 * 暂停播放
	 */
	public void pauseBackgroundMusic() {
		if (mBackgroundMediaPlayer != null
				&& mBackgroundMediaPlayer.isPlaying()) {
			mBackgroundMediaPlayer.pause();
			this.mIsPaused = true;
		}
	}

	/**
	 * 继续播放
	 */
	public void resumeBackgroundMusic() {
		if (mBackgroundMediaPlayer != null && this.mIsPaused) {
			mBackgroundMediaPlayer.start();
			this.mIsPaused = false;
		}
	}

	/**
	 * 重新播放
	 */
	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");
			}
		}
	}

	
	public boolean isBackgroundMusicPlaying() {
		boolean ret = false;
		if (mBackgroundMediaPlayer == null) {
			ret = false;
		} else {
			ret = mBackgroundMediaPlayer.isPlaying();
		}
		return ret;
	}

	
	public void end() {
		if (mBackgroundMediaPlayer != null) {
			mBackgroundMediaPlayer.release();
		}
		
		initData();
	}

	
	
	public void setBackgroundVolume(float volume) {
		this.mLeftVolume = this.mRightVolume = volume;
		if (this.mBackgroundMediaPlayer != null) {
			this.mBackgroundMediaPlayer.setVolume(this.mLeftVolume,
					this.mRightVolume);
		}
	}

	
}</span></strong>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值