背景音乐功能

  2 import android.content.Context;
  3 import android.content.res.AssetFileDescriptor;
  4 import android.media.MediaPlayer;
  5 import android.util.Log;
  6 
  7 /**
  8  * 
  9  * This class is used for controlling background music
 10  *
 11  */
 12 public class BackgroundMusic {
 13     
 14     private static final String TAG = "Bg_Music";
 15     private float mLeftVolume;
 16     private float mRightVolume;
 17     private Context mContext;
 18     private MediaPlayer mBackgroundMediaPlayer;
 19     private boolean mIsPaused;
 20     private String mCurrentPath;
 21 
 22     public BackgroundMusic(Context context){
 23         this.mContext = context;
 24         initData();
 25     }
 26     //初始化一些数据
 27     private void initData(){
 28             mLeftVolume =0.5f;
 29             mRightVolume = 0.5f;
 30             mBackgroundMediaPlayer = null;
 31             mIsPaused = false;
 32             mCurrentPath = null;
 33     }
 34     
 35     /**
 36      * 根据path路径播放背景音乐
 37      * @param path :assets中的音频路径
 38      * @param isLoop  :是否循环播放
 39      */
 40     public void playBackgroundMusic(String path, boolean isLoop){
 41         if (mCurrentPath == null){
 42             //这是第一次播放背景音乐--- it is the first time to play background music
 43             //或者是执行end()方法后,重新被叫---or end() was called
 44             mBackgroundMediaPlayer = createMediaplayerFromAssets(path);    
 45             mCurrentPath = path;
 46         } 
 47         else {
 48             if (! mCurrentPath.equals(path)){
 49                 //播放一个新的背景音乐--- play new background music
 50                 //释放旧的资源并生成一个新的----release old resource and create a new one
 51                 if (mBackgroundMediaPlayer != null){
 52                     mBackgroundMediaPlayer.release();                
 53                 }                
 54                 mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
 55                 //记录这个路径---record the path
 56                 mCurrentPath = path;
 57               }
 58         }
 59         
 60         if (mBackgroundMediaPlayer == null){
 61             Log.e(TAG, "playBackgroundMusic: background media player is null");
 62         } else {        
 63             // 若果音乐正在播放或已近中断,停止它---if the music is playing or paused, stop it
 64             mBackgroundMediaPlayer.stop();            
 65             mBackgroundMediaPlayer.setLooping(isLoop);            
 66             try {
 67                 mBackgroundMediaPlayer.prepare();
 68                 mBackgroundMediaPlayer.seekTo(0);
 69                 mBackgroundMediaPlayer.start();                
 70                 this.mIsPaused = false;
 71             } catch (Exception e){
 72                 Log.e(TAG, "playBackgroundMusic: error state");
 73             }            
 74         }
 75     }
 76     
 77     /**
 78      * 停止播放背景音乐
 79      */
 80     public void stopBackgroundMusic(){
 81         if (mBackgroundMediaPlayer != null){
 82             mBackgroundMediaPlayer.stop();
 83             // should set the state, if not , the following sequence will be error
 84             // play -> pause -> stop -> resume
 85             this.mIsPaused = false;
 86         }
 87     }
 88     /**
 89      * 暂停播放背景音乐
 90      */
 91     public void pauseBackgroundMusic(){        
 92         if (mBackgroundMediaPlayer != null && mBackgroundMediaPlayer.isPlaying()){
 93             mBackgroundMediaPlayer.pause();
 94             this.mIsPaused = true;
 95         }
 96     }
 97     /**
 98      * 继续播放背景音乐
 99      */
100     public void resumeBackgroundMusic(){
101         if (mBackgroundMediaPlayer != null && this.mIsPaused){
102             mBackgroundMediaPlayer.start();
103             this.mIsPaused = false;
104         }
105     }
106     /**
107      * 重新播放背景音乐
108      */
109     public void rewindBackgroundMusic(){        
110         if (mBackgroundMediaPlayer != null){
111             mBackgroundMediaPlayer.stop();            
112             try {
113                 mBackgroundMediaPlayer.prepare();
114                 mBackgroundMediaPlayer.seekTo(0);
115                 mBackgroundMediaPlayer.start();
116                 this.mIsPaused = false;
117             } catch (Exception e){
118                 Log.e(TAG, "rewindBackgroundMusic: error state");
119             }            
120         }
121     }
122     /**
123      * 判断背景音乐是否正在播放
124      * @return:返回的boolean值代表是否正在播放
125      */
126     public boolean isBackgroundMusicPlaying(){
127         boolean ret = false;
128         if (mBackgroundMediaPlayer == null){
129             ret = false;
130         } else {
131             ret = mBackgroundMediaPlayer.isPlaying();
132         }
133         return ret;
134     }
135     /**
136      * 结束背景音乐,并释放资源
137      */
138     public void end(){
139         if (mBackgroundMediaPlayer != null){
140             mBackgroundMediaPlayer.release();
141         }
142         //重新“初始化数据”
143         initData();
144     }
145     /**
146      * 得到背景音乐的“音量”
147      * @return
148      */
149     public float getBackgroundVolume(){
150         if (this.mBackgroundMediaPlayer != null){
151             return (this.mLeftVolume + this.mRightVolume) / 2;
152         } else {
153             return 0.0f;
154         }
155     }
156     /**
157      * 设置背景音乐的音量
158      * @param volume:设置播放的音量,float类型
159      */
160     public void setBackgroundVolume(float volume){
161         this.mLeftVolume = this.mRightVolume = volume;
162         if (this.mBackgroundMediaPlayer != null){
163             this.mBackgroundMediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume);
164         }
165     }
166     /**
167      * create mediaplayer for music
168      * @param path the path relative to assets
169      * @return 
170      */
171     private MediaPlayer createMediaplayerFromAssets(String path){
172         MediaPlayer mediaPlayer = null;
173         try{            
174             AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path);
175             mediaPlayer = new MediaPlayer();
176             mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), 
177                     assetFileDescritor.getStartOffset(), assetFileDescritor.getLength());
178             mediaPlayer.prepare();
179             mediaPlayer.setVolume(mLeftVolume, mRightVolume);
180         }catch (Exception e) {
181             mediaPlayer = null;
182             Log.e(TAG, "error: " + e.getMessage(), e);
183         }
184         return mediaPlayer;
185     }
186 }
187 
188  

 

posted @ 2015-07-16 21:42  炫酷叼咋天  阅读(165)  评论(0编辑  收藏  举报