详细介绍手机游戏中的声音处理
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
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
相关文章推荐
- 手机游戏中声音的播放及处理
- J2me Game开发技巧:手机游戏中声音播放及处理
- 转帖 -详细介绍声音处理
- 手机游戏中声音的播放及处理
- 安卓手机测试必备技能——ADB详细介绍
- 超详细介绍 图像处理(卷积)
- 【Android LibGDX游戏引擎开发教程】第01期:LibGDX的详细介绍
- (Linux文本处理神指令)sed介绍.附详细例子(下)
- A Detailed Review of Feature Extraction in Image Processing Systems图像处理系统中特征提取的详细介绍
- 游戏关卡设计的详细介绍
- 随身的娱乐!PPC游戏模拟器详细介绍[转贴]
- 详细介绍mysql-bin.000001文件的来源及处理方法
- DrawerLayout的使用 侧滑加载Listview,首页使用Fragment和处理点击触摸冲突、穿透事件和监听菜单滑动状态的改变的详细介绍
- 【手机网络游戏 编程】C#异步socketAPI调用 处理数据的流程
- cocos2dx-lua 在cocos2d中使用lua脚本进行开发,并介绍脚本在游戏中的详细用途
- C#扫雷游戏详细介绍
- ios 滤镜处理(详细滤镜介绍)及处理方法
- PHP cdata 处理(详细介绍)
- 同时处理消息和内核对象详细介绍
- 详细介绍个人电脑中毒后的紧急处理措施