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

本文介绍了一款游戏在不同手机型号上实现声音处理的方法,包括初始化声音、播放和停止声音的过程,并针对不同型号手机进行了适配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值