android平台俄罗斯方块游戏完整代码
2012-12-02 14:31:58 来源:WEB开发网核心提示: 音乐播放类,MusicPlay.java 具体代码如下:package com.example.eluosifangkuai;import java.util.HashMap;import java.util.Map;import android.content.Context;import android.medi
音乐播放类,MusicPlay.java 具体代码如下:
package com.example.eluosifangkuai;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
public class MusicPlay {
public static MediaPlayer m_MediaPlay ; // 背景播放器
public static MediaPlayer m_MenuPlay ;
public static SoundPool soundPool;//声明 音效播放器
public MediaPlayer m_FastDown ;
private static boolean musicSwitch = true;//音乐开关
private static boolean soundSwitch = true;//音效开关
private static Map<Integer,Integer> soundMap; //音效资源id与加载过后的音源id的映射关系表
private static Context context;
public static void inItMusicPlay(Context c){
context = c;
}
public static void inItMenuMusicPlay(Context c){
context = c;
}
//初始化背景播放器
public static void BgMediaplayer()
{
m_MediaPlay = MediaPlayer.create(context, R.raw.gamebg);
m_MediaPlay.setLooping(true);
}
public static void menuMusic()
{
m_MenuPlay = MediaPlayer.create(context, R.raw.menubg);
m_MenuPlay.setLooping(true);
}
public static void pauseMusic()
{
if(m_MediaPlay.isPlaying())
{
m_MediaPlay.pause();
}
}
public static void pauseMenuMusic()
{
if(m_MenuPlay.isPlaying())
{
m_MenuPlay.pause();
}
}
public static void startMusic(){
if(musicSwitch){
m_MediaPlay.start();
}
}
public static void startMenuMusic(){
if(musicSwitch){
m_MenuPlay.start();
}
}
public static void releaseMusic(){
if(m_MediaPlay != null){
m_MediaPlay.release();
}
}
public static void releaseMenuMusic(){
if(m_MenuPlay != null)
{
m_MenuPlay.release();
}
}
//设置音乐开关
public static void setMusicSwitch(boolean musicSwitch){
MusicPlay.musicSwitch = musicSwitch;
if(MusicPlay.musicSwitch){
m_MediaPlay.start();
}
else{
m_MediaPlay.stop();
}
}
public static void inItSound()
{
soundPool = new SoundPool(8,AudioManager.STREAM_MUSIC,0);
soundMap = new HashMap<Integer, Integer>();
//将音效资源加入 soundPool,并做成soundMap 映射
soundMap.put(R.raw.action,soundPool.load(context, R.raw.action, 1));
soundMap.put(R.raw.fastdown,soundPool.load(context, R.raw.fastdown, 1));
soundMap.put(R.raw.rotation,soundPool.load(context, R.raw.rotation, 1));
soundMap.put(R.raw.down,soundPool.load(context, R.raw.down, 1));
soundMap.put(R.raw.delete1,soundPool.load(context, R.raw.delete1, 1));
soundMap.put(R.raw.delete2,soundPool.load(context, R.raw.delete2, 1));
soundMap.put(R.raw.delete3,soundPool.load(context, R.raw.delete3, 1));
soundMap.put(R.raw.delete4,soundPool.load(context, R.raw.delete4, 1));
}
public static int playSound(int resId,int loop)
{
if(!soundSwitch){
return 0;
}
Integer soundId = soundMap.get(resId);
if(soundId != null)
{
return soundPool.play(soundId, 1, 1, 1, loop, 1);
}
else
{
return 0;
}
}
public static void releaseSound(){
if(soundPool != null){
soundPool.release();
}
}
}
public static MediaPlayer m_MediaPlay ; // 背景播放器
public static MediaPlayer m_MenuPlay ;
public static SoundPool soundPool;//声明 音效播放器
public MediaPlayer m_FastDown ;
private static boolean musicSwitch = true;//音乐开关
private static boolean soundSwitch = true;//音效开关
private static Map<Integer,Integer> soundMap; //音效资源id与加载过后的音源id的映射关系表
private static Context context;
public static void inItMusicPlay(Context c){
context = c;
}
public static void inItMenuMusicPlay(Context c){
context = c;
}
//初始化背景播放器
public static void BgMediaplayer()
{
m_MediaPlay = MediaPlayer.create(context, R.raw.gamebg);
m_MediaPlay.setLooping(true);
}
public static void menuMusic()
{
m_MenuPlay = MediaPlayer.create(context, R.raw.menubg);
m_MenuPlay.setLooping(true);
}
public static void pauseMusic()
{
if(m_MediaPlay.isPlaying())
{
m_MediaPlay.pause();
}
}
public static void pauseMenuMusic()
{
if(m_MenuPlay.isPlaying())
{
m_MenuPlay.pause();
}
}
public static void startMusic(){
if(musicSwitch){
m_MediaPlay.start();
}
}
public static void startMenuMusic(){
if(musicSwitch){
m_MenuPlay.start();
}
}
public static void releaseMusic(){
if(m_MediaPlay != null){
m_MediaPlay.release();
}
}
public static void releaseMenuMusic(){
if(m_MenuPlay != null)
{
m_MenuPlay.release();
}
}
//设置音乐开关
public static void setMusicSwitch(boolean musicSwitch){
MusicPlay.musicSwitch = musicSwitch;
if(MusicPlay.musicSwitch){
m_MediaPlay.start();
}
else{
m_MediaPlay.stop();
}
}
public static void inItSound()
{
soundPool = new SoundPool(8,AudioManager.STREAM_MUSIC,0);
soundMap = new HashMap<Integer, Integer>();
//将音效资源加入 soundPool,并做成soundMap 映射
soundMap.put(R.raw.action,soundPool.load(context, R.raw.action, 1));
soundMap.put(R.raw.fastdown,soundPool.load(context, R.raw.fastdown, 1));
soundMap.put(R.raw.rotation,soundPool.load(context, R.raw.rotation, 1));
soundMap.put(R.raw.down,soundPool.load(context, R.raw.down, 1));
soundMap.put(R.raw.delete1,soundPool.load(context, R.raw.delete1, 1));
soundMap.put(R.raw.delete2,soundPool.load(context, R.raw.delete2, 1));
soundMap.put(R.raw.delete3,soundPool.load(context, R.raw.delete3, 1));
soundMap.put(R.raw.delete4,soundPool.load(context, R.raw.delete4, 1));
}
public static int playSound(int resId,int loop)
{
if(!soundSwitch){
return 0;
}
Integer soundId = soundMap.get(resId);
if(soundId != null)
{
return soundPool.play(soundId, 1, 1, 1, loop, 1);
}
else
{
return 0;
}
}
public static void releaseSound(){
if(soundPool != null){
soundPool.release();
}
}
}
更多精彩
赞助商链接