博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
详细介绍手机游戏中的声音处理
阅读量:4045 次
发布时间:2019-05-24

本文共 5926 字,大约阅读时间需要 19 分钟。

本文是在同一个游戏中移植在不同机型时所做的对声音的处理,考虑到性能的要求,对每种类型的手机做了一定的要求

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

转载地址:http://hbedi.baihongyu.com/

你可能感兴趣的文章
在C++中使用Lua
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>
GitHub 万星推荐:黑客成长技术清单
查看>>
可以在线C++编译的工具站点
查看>>
关于无人驾驶的过去、现在以及未来,看这篇文章就够了!
查看>>
所谓的进步和提升,就是完成认知升级
查看>>
为什么读了很多书,却学不到什么东西?
查看>>
长文干货:如何轻松应对工作中最棘手的13种场景?
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>
No.147 - LeetCode1108
查看>>
No.174 - LeetCode1305 - 合并两个搜索树
查看>>
No.175 - LeetCode1306
查看>>
No.176 - LeetCode1309
查看>>
No.182 - LeetCode1325 - C指针的魅力
查看>>
mysql:sql alter database修改数据库字符集
查看>>
mysql:sql truncate (清除表数据)
查看>>
yuv to rgb 转换失败呀。天呀。谁来帮帮我呀。
查看>>
yuv420 format
查看>>
yuv420 还原为RGB图像
查看>>