<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>
背景音乐功能
最新推荐文章于 2018-11-21 15:10:32 发布