【文件属性】:
文件名称:android手机游戏源码
文件大小:2.04MB
文件格式:ZIP
更新时间:2016-07-14 13:02:04
android 手机游戏 源码
手机游戏源码,可运行,比较好
package com.itcast;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.media.MediaPlayer;
public class SoundService {
Map sounds = new HashMap();
Context context = null;
public SoundService(Context context) {
this.context = context;
}
public void play(int resId) {
MediaPlayer mp = sounds.get(resId);
if (mp == null) {
mp = MediaPlayer.create(context, resId);
if (mp == null) {
return;
}
sounds.put(resId, mp);
}
if (mp.isPlaying()) {
mp.reset();
try {
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
mp.start();
}
}
public void release() {
for(Map.Entry entry : sounds.entrySet())
{
MediaPlayer mp = entry.getValue();
if (mp != null)
{
mp.release();
}
}
}
}