android實(shí)現(xiàn)背景音樂播放功能
參考了一下網(wǎng)上別人寫的,再使用的時(shí)候是放在新開的線程中來播放音樂的,后來發(fā)現(xiàn)每次進(jìn)入Activity后就會(huì)重復(fù)開始一個(gè)音樂播放的聲音。為了避免重復(fù)開啟播放功能我在原來代碼的基礎(chǔ)上增加了單例模式。這樣就避免了出現(xiàn)重復(fù)播放。
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 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;
}
// 初始化一些數(shù)據(jù)
private void initData() {
mLeftVolume = 0.5f;
mRightVolume = 0.5f;
mBackgroundMediaPlayer = null;
mIsPaused = false;
mCurrentPath = null;
}
/**
* 根據(jù)path路徑播放背景音樂
*
* @param path
* :assets中的音頻路徑
* @param isLoop
* :是否循環(huán)播放
*/
public void playBackgroundMusic(String path, boolean isLoop) {
if (mCurrentPath == null) {
// 這是第一次播放背景音樂--- it is the first time to play background music
// 或者是執(zhí)行end()方法后,重新被叫---or end() was called
mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
mCurrentPath = path;
} else {
if (!mCurrentPath.equals(path)) {
// 播放一個(gè)新的背景音樂--- play new background music
// 釋放舊的資源并生成一個(gè)新的----release old resource and create a new one
if (mBackgroundMediaPlayer != null) {
mBackgroundMediaPlayer.release();
}
mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
// 記錄這個(gè)路徑---record the path
mCurrentPath = path;
}
}
if (mBackgroundMediaPlayer == null) {
Log.e(TAG, "playBackgroundMusic: background media player is null");
} else {
// 若果音樂正在播放或已近中斷,停止它---if the music is playing or paused, stop it
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;
}
}
/**
* 繼續(xù)播放背景音樂
*/
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");
}
}
}
/**
* 判斷背景音樂是否正在播放
*
* @return:返回的boolean值代表是否正在播放
*/
public boolean isBackgroundMusicPlaying() {
boolean ret = false;
if (mBackgroundMediaPlayer == null) {
ret = false;
} else {
ret = mBackgroundMediaPlayer.isPlaying();
}
return ret;
}
/**
* 結(jié)束背景音樂,并釋放資源
*/
public void end() {
if (mBackgroundMediaPlayer != null) {
mBackgroundMediaPlayer.release();
}
// 重新“初始化數(shù)據(jù)”
initData();
}
/**
* 得到背景音樂的“音量”
*
* @return
*/
public float getBackgroundVolume() {
if (this.mBackgroundMediaPlayer != null) {
return (this.mLeftVolume + this.mRightVolume) / 2;
} else {
return 0.0f;
}
}
/**
* 設(shè)置背景音樂的音量
*
* @param volume
* :設(shè)置播放的音量,float類型
*/
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;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用Service實(shí)現(xiàn)簡(jiǎn)單音樂播放實(shí)例
- Android版音樂播放器
- android實(shí)現(xiàn)音樂播放器進(jìn)度條效果
- Android實(shí)現(xiàn)簡(jiǎn)單音樂播放控件
- 教你輕松制作Android音樂播放器
- android暫?;蛲V蛊渌魳凡シ牌鞯牟シ艑?shí)現(xiàn)代碼
- Android編程開發(fā)音樂播放器實(shí)例
- Android簡(jiǎn)易音樂播放器實(shí)現(xiàn)代碼
- Android MediaPlayer實(shí)現(xiàn)音樂播放器實(shí)例代碼
- android音樂播放器監(jiān)聽電話狀態(tài)實(shí)現(xiàn)代碼
相關(guān)文章
Android實(shí)現(xiàn)點(diǎn)匯聚成字的動(dòng)態(tài)效果詳解
在引入?fl_chart?繪制圖表的時(shí)候,看到插件有下面這樣的動(dòng)效,隨機(jī)散亂的圓點(diǎn)最后組合成了?Flutter?的?Logo,挺酷炫的。本篇我們來探討類似的效果怎么實(shí)現(xiàn)2022-07-07
RecyclerView Adapter輔助類詳解及示例代碼
本文主要介紹RecyclerView Adapter輔助類的知識(shí),這里整理了詳細(xì)資料及簡(jiǎn)單示例代碼,幫助大家學(xué)習(xí)這部分的內(nèi)容,有興趣的小伙伴可以參考下2016-09-09
android開發(fā)教程之實(shí)現(xiàn)listview下拉刷新和上拉刷新效果
這篇文章主要介紹了android實(shí)現(xiàn)listview下拉刷新和上拉刷新效果,Android的ListView上拉下拉刷新,原理都一樣,在Touch事件中操作header/footer的paddingTop屬性,需要的朋友可以參考下2014-02-02
Android系統(tǒng)添加Linux驅(qū)動(dòng)
今天小編就為大家分享一篇關(guān)于Android系統(tǒng)添加Linux驅(qū)動(dòng)的文章,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
Android DrawerLayout帶有側(cè)滑功能的布局類(1)
這篇文章主要為大家詳細(xì)介紹了Android DrawerLayout帶有側(cè)滑功能的布局類,感興趣的小伙伴們可以參考一下2016-07-07
Android之Viewpager+Fragment實(shí)現(xiàn)懶加載示例
本篇文章主要介紹了Android之Viewpager+Fragment實(shí)現(xiàn)懶加載示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03

