android实现背景音乐播放
生活随笔
收集整理的這篇文章主要介紹了
android实现背景音乐播放
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考了一下網上比人寫的,再使用的時候是放在新開的線程中來播放音樂的,后來發現每次進入Activity后就會重復開始一個音樂播放的聲音。為了避免重復開啟播放功能我在原來代碼的基礎上增加了單例模式。這樣就避免了出現重復播放。
[java] view plaincopy 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;?? ????}?? ?? ????//?初始化一些數據?? ????private?void?initData()?{?? ????????mLeftVolume?=?0.5f;?? ????????mRightVolume?=?0.5f;?? ????????mBackgroundMediaPlayer?=?null;?? ????????mIsPaused?=?false;?? ????????mCurrentPath?=?null;?? ????}?? ?? ????/**? ?????*?根據path路徑播放背景音樂? ?????*?? ?????*?@param?path? ?????*????????????:assets中的音頻路徑? ?????*?@param?isLoop? ?????*????????????:是否循環播放? ?????*/?? ????public?void?playBackgroundMusic(String?path,?boolean?isLoop)?{?? ????????if?(mCurrentPath?==?null)?{?? ????????????//?這是第一次播放背景音樂---?it?is?the?first?time?to?play?background?music?? ????????????//?或者是執行end()方法后,重新被叫---or?end()?was?called?? ????????????mBackgroundMediaPlayer?=?createMediaplayerFromAssets(path);?? ????????????mCurrentPath?=?path;?? ????????}?else?{?? ????????????if?(!mCurrentPath.equals(path))?{?? ????????????????//?播放一個新的背景音樂---?play?new?background?music?? ????????????????//?釋放舊的資源并生成一個新的----release?old?resource?and?create?a?new?one?? ????????????????if?(mBackgroundMediaPlayer?!=?null)?{?? ????????????????????mBackgroundMediaPlayer.release();?? ????????????????}?? ????????????????mBackgroundMediaPlayer?=?createMediaplayerFromAssets(path);?? ????????????????//?記錄這個路徑---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;?? ????????}?? ????}?? ?? ????/**? ?????*?繼續播放背景音樂? ?????*/?? ????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;?? ????}?? ?? ????/**? ?????*?結束背景音樂,并釋放資源? ?????*/?? ????public?void?end()?{?? ????????if?(mBackgroundMediaPlayer?!=?null)?{?? ????????????mBackgroundMediaPlayer.release();?? ????????}?? ????????//?重新“初始化數據”?? ????????initData();?? ????}?? ?? ????/**? ?????*?得到背景音樂的“音量”? ?????*?? ?????*?@return? ?????*/?? ????public?float?getBackgroundVolume()?{?? ????????if?(this.mBackgroundMediaPlayer?!=?null)?{?? ????????????return?(this.mLeftVolume?+?this.mRightVolume)?/?2;?? ????????}?else?{?? ????????????return?0.0f;?? ????????}?? ????}?? ?? ????/**? ?????*?設置背景音樂的音量? ?????*?? ?????*?@param?volume? ?????*????????????:設置播放的音量,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;?? ????}?? }?
總結
以上是生活随笔為你收集整理的android实现背景音乐播放的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux lftp上传文件,linux
- 下一篇: python多个对象调用类方法、且之间有