您的位置:首页 > 移动开发 > Android开发

实现游戏音效的预加载

2015-11-25 23:03 591 查看
游戏中有很多东西是需要预加载进来的,这样可以使游戏更流畅,虽然,游戏中音效占用的资源比较少,但是,还是有必要为音效做一个预加载的,这里用安卓原生的方法实现了一个音效加载系统。

思路:

安卓中游戏的资源通常都是根据id来加载的(这个id可以在R.java文件中找到),在系统中使用了两个HashMap,一个存储资源的ID,一个存储音效加载后的ID,这两个HashMap有共同的key值将两个表联系起来。

系统中实现了资源的加载,根据资源ID播放音效,卸载资源。

下面是脑图的地址,是系统中详细函数的介绍。
http://naotu.baidu.com/file/079133fa68a2e51d5800f905e77ef67b?token=d52654a7a8883f62


Demo代码:

MainActivity.java

package com.example.soundloadsystem;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
public static MainActivity _instance ;
public SoundLoadSystem soundLoadSystem;
public SoundPool m_soundPool ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setContentView(R.layout.main);
_instance = this;
m_soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
soundLoadSystem = SoundLoadSystem.Instance();
addSoundRes();
initUICallback();
}

public void addSoundRes()
{
soundLoadSystem.add(R.raw.sound_0);
soundLoadSystem.add(R.raw.sound_1);
soundLoadSystem.add(R.raw.sound_2);
soundLoadSystem.add(R.raw.sound_3);
soundLoadSystem.add(R.raw.sound_4);
soundLoadSystem.add(R.raw.sound_5);
}

public void initUICallback()
{
Button btn1 = (Button)findViewById(R.id.Button01);
Button btn2 = (Button)findViewById(R.id.Button02);
Button btn3 = (Button)findViewById(R.id.Button04);
btn1.setOnClickListener(
new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
soundLoadSystem.load();
}
}
);
btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub\
AudioManager am = (AudioManager)_instance.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

float volume = streamVolumeCurrent / streamVolumeMax;

m_soundPool.play(soundLoadSystem.getRandomID(), volume, volume, 1, 0, 1.0f);
}
});

btn3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
soundLoadSystem.unload();
}
});
}
}


SoundLoadSystem.java
package com.example.soundloadsystem;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import android.R.integer;
import android.content.Context;
import android.drm.DrmStore.RightsStatus;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

public class SoundLoadSystem
{
public static SoundLoadSystem _instance ;
public HashMap<Integer, Integer> m_soundResIDMap ;
public HashMap<Integer, Integer> m_soundIDMap;
public int m_index = 0 ;
public static SoundLoadSystem Instance()
{
if (_instance == null) {
_instance = new SoundLoadSystem();
}
return _instance ;
}
public static void Destory()
{
_instance = null ;
System.gc();
}
private SoundLoadSystem() {
// TODO Auto-generated constructor stub
m_soundResIDMap = new HashMap<Integer, Integer>();
m_soundIDMap = new HashMap<Integer, Integer>();
}
public void add(Integer id)
{
if (isHave(id) == false) {
m_soundResIDMap.put(m_index, id);
m_index++ ;
}
}
public void load()
{
Context cot = MainActivity._instance ;
SoundPool spPool = MainActivity._instance.m_soundPool;
int per = 0 ;
Iterator iterator = m_soundResIDMap.entrySet().iterator() ;
while (iterator.hasNext()) {
Map.Entry<Integer,Integer> entry = (Map.Entry<Integer,Integer>)iterator.next();
Integer index = entry.getKey();
Integer id = entry.getValue();
Integer spID = spPool.load(cot, id,1);
m_soundIDMap.put(index, spID);
per++;
}
}
public int getLength()
{
//Because m_index start from 0,so the length is m_index + 1.
return m_soundIDMap.size();
}
public boolean isHave(Integer id)
{
return m_soundResIDMap.containsValue(id) ;
}
public Integer getRandomID()
{
if (m_soundIDMap.size() == 0
|| m_soundResIDMap.size() == 0 )
{
Log.e("load_error","Doesn't load any res.");
return -1;
}
Integer ret = -1;
Random random = new Random();
Integer randInteger = random.nextInt(getLength());
ret = m_soundIDMap.get(randInteger);
return ret;
}

public void unload()
{
SoundPool spPool = MainActivity._instance.m_soundPool;
spPool.unload(1);
Iterator iterator = m_soundIDMap.entrySet().iterator() ;
while (iterator.hasNext()) {
Map.Entry<Integer,Integer> entry = (Map.Entry)iterator.next();
Integer id = entry.getValue();
spPool.unload(id);
}
m_soundIDMap.clear();
m_soundIDMap = new HashMap<Integer, Integer>();
}

public int getSoundIDByResID(Integer resID)
{
int soundID = -1;
int index = -1;
boolean isHave = m_soundResIDMap.containsKey(resID);
if (isHave) {
Iterator iterator = m_soundIDMap.entrySet().iterator() ;
while (iterator.hasNext()) {
Map.Entry<Integer,Integer> entry = (Map.Entry)iterator.next();
Integer idx = entry.getKey();
Integer id = entry.getValue();
if (id == resID) {
index = idx ;
}
}
soundID = m_soundIDMap.get(index);
}
else {
Log.e("sound_res","can't find sound which id is" + resID);
}
return soundID ;
}
public void reset()
{
m_soundIDMap.clear();
m_soundResIDMap.clear();
m_soundIDMap = new HashMap<Integer, Integer>();
m_soundResIDMap = new HashMap<Integer, Integer>();
m_index = 0;
}
}


Demo下载地址:http://download.csdn.net/detail/c_boy_lu/9300661
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息