您的位置:首页 > 其它

详细介绍手机游戏中的声音处理

2009-02-05 08:45 246 查看
本文是在同一个游戏中移植在不同机型时所做的对声音的处理,考虑到性能的要求,对每种类型的手机做了一定的要求

s40 中的声音处理:

1) import com.nokia.mid.sound.*;

2)

sound soundplayer;
void initsound(){
soundplayer = new sound(b_main,1);
if(m_playsound == 1){
soundplayer.play(0);
}
}

3)
byte[] b_main = {
(byte)0x02,(byte)0x4a,(byte)0x3a,(byte)0x40,
(byte)0x04,(byte)0x01,(byte)0x1f,(byte)0x1e,
(byte)0x54,(byte)0x88,(byte)0x38,(byte)0x84,
(byte)0x44,(byte)0xbc,(byte)0x4a,(byte)0xc4,
(byte)0xa0,(byte)0xa9,(byte)0x0b,(byte)0x91,
(byte)0x27,(byte)0x22,(byte)0xa2,(byte)0xb1,
(byte)0x31,(byte)0x13,(byte)0x88,(byte)0x00,
};

4)
static int m_playsound = 1;

5) 在程序中对声音的控制
m_playsound = (byte)(1 - m_playsound);
if(m_playsound == 1){
try{
soundplayer.play(0);
} catch(exception e){}
}
if(m_playsound == 0){
try{
soundplayer.stop();
} catch(exception e){}
}

//----------------------------------------------------

使用 ott 文件 在nokia 40或 60中

1) 定义数据结构
public class emsound
{
public int type;
public byte[] data;

public emsound(byte[] data, int type)
{
this.type = type;
this.data = data;
}
}

2)
import com.nokia.mid.ui.*;
import com.nokia.mid.sound.*;
3)
sound soundplayer;
soundlistener soundlistener = new emsoundlistener();

emsound currentsound = null;
boolean soundplaying = false;
boolean soundenable = true;

class emsoundlistener
implements soundlistener {
public void soundstatechanged(sound sound, int event) {
switch (event) {
case sound.sound_stopped:
soundplaying = false;
break;
case sound.sound_playing:
soundplaying = true;
}
}
}

public emsound loadsound(string resfile, int resid) {
emsound sound;
try {
inputstream is = getclass().getresourceasstream(resfile + "/" + resid +
".ott");
int len = (int) is.skip(10000);
is.close();
is = getclass().getresourceasstream(resfile + "/" + resid + ".ott");
byte[] barr = new byte[len];
is.read(barr);
is.close();
sound = new emsound(barr, sound.format_tone);
}
catch (exception ex) {
sound = null;
}
return sound;
}

public void playsound(emsound sound, int count) {
if (!soundenable) {
return;
}
try { //colico
if (soundplaying) {
stopsound();
}
if (soundplayer == null) {
soundplayer = new sound(sound.data, sound.type);
soundplayer.setsoundlistener(soundlistener);
currentsound = null;
}
if (sound != currentsound) {
soundplayer.release();
soundplayer.init(sound.data, sound.type);
currentsound = sound;
}

soundplayer.play(count);
}
catch (exception ex) {
soundplaying = false;
}
}

sound[] soundplayers;
public void playsound( emsound sound[], int loc)
{
if (!soundenable) { return; }

try {
if (soundplaying) stopsound();
if (soundplayers == null) {
soundplayers = new sound[sound.length];
system.out.println("sounds == null");
for (int i=0; i<sound.length ; i++ ){
soundplayers[i] = new sound( sound[i].data, sound[i].type );
soundplayers[i].setsoundlistener( soundlistener );
soundplayers[i].init(sound[i].data, sound[i].type);
}
}

long now = system.currenttimemillis();
soundplayers[loc].play(1);
system.out.println("playing sounds");
system.out.println("playing sounds time"+(system.currenttimemillis()-now) );
} catch(exception ex) {
soundplaying = false;
}
}

public void stopsound() {
if (!soundenable) {
return;
}
if (soundplayer != null) { //colico
soundplayer.stop();
}
}

public boolean issoundplaying() {
return soundplaying;
}

public boolean issoundenable() {
return soundenable;
}

public void setsoundenable(boolean e) {
if (!e) {
stopsound();
}
soundenable = e;
}

在v300中
1).
public class emsound
{
public string type;
public byte[] data;

public emsound(byte[] data, string type)
{
this.type = type;
this.data = data;
}

}

2).

import javax.microedition.media.player;
import javax.microedition.media.playerlistener;
import javax.microedition.media.manager;
import javax.microedition.media.control.*;

3). //sound soundplayer;
playerlistener soundlistener = new emsoundlistener();
player soundplayer;
emsound currentsound = null;
boolean soundplaying = false;
boolean soundenable = true;

class emsoundlistener
implements playerlistener {

public void playerupdate(player player, string event, object eventdata) { //soundstatechanged(int event)
if (event == playerlistener.stopped) {
soundplaying = false;
}
if (event == playerlistener.started) {
soundplaying = true;
}
}
}

public emsound loadsound(string resfile, int resid) {
emsound sound;
try {
inputstream is = getclass().getresourceasstream(resfile + "/" + resid +
".mid");
int len = (int) is.skip(10000);
is.close();
is = getclass().getresourceasstream(resfile + "/" + resid + ".mid");
byte[] barr = new byte[len];
is.read(barr);
is.close();
sound = new emsound(barr, "audio/midi");
}
catch (exception ex) {
sound = null;
}
return sound;
}

public void playsound(emsound sound, int count) {
if (!soundenable) {
return;
}
try {
if (soundplaying) {
stopsound();
}
if (soundplayer == null) {
soundplayer = manager.createplayer(new bytearrayinputstream(sound.data),
sound.type);
soundplayer.addplayerlistener(soundlistener);
currentsound = null;
}
if (sound != currentsound) {
soundplayer.close();
soundplayer = manager.createplayer(new bytearrayinputstream(sound.data),
sound.type);
currentsound = sound;
}
soundplayer.start();
}
catch (exception ex) {
soundplaying = false;
system.out.println(ex.tostring());
}
}

public void stopsound() {
if (!soundenable) {
return;
}
if (soundplayer != null) {
try {
soundplayer.stop();
}
catch (exception e) {
system.out.print(e.tostring());
}
}
}

public boolean issoundplaying() {
return soundplaying;
}

public boolean issoundenable() {
return soundenable;
}

3.读取mid文件

1)
import javax.microedition.media.*;

2)

player player;
void initsound() {
try {
player = manager.createplayer(getstream("/sound/b_main.mid"),
"audio/midi");
player.realize();
player.setloopcount(100000);
}
catch (exception e) {
e.printstacktrace();
}

}

3) //在程序中对声音的控制

m_playsound = (byte) (1 - m_playsound);
if (m_playsound == 1) {
try {
player.start();
}
catch (exception e) {}
}
if (m_playsound == 0) {
try {
player.stop();

}

catch (exception e) {}
}

///---------------end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: