码迷,mamicode.com
首页 > 其他好文 > 详细

背景音乐功能

时间:2015-07-04 12:35:08      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

   下面是实现此功能的具体代码:

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


根据代码实现了此功能

背景音乐功能

标签:

原文地址:http://www.cnblogs.com/lzk101816/p/4620439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!