选择显示字体大小

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

详细介绍声音处理
  /**
   *    作者 colico    email:colico@163.com
   *    http://blog.csdn.net/colico
   *    http://colico.ys168.com
   *   注:此为 我是小o 原创,需要转载请附上以上信息。
   *
   */


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

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(&quot;playing sounds&quot;);
            system.out.println(&quot;playing sounds time&quot;+(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 + &quot;/&quot; + resid +
          &quot;.mid&quot;);
      int len = (int) is.skip(10000);
      is.close();
      is = getclass().getresourceasstream(resfile + &quot;/&quot; + resid + &quot;.mid&quot;);
      byte[] barr = new byte[len];
      is.read(barr);
      is.close();
      sound = new emsound(barr, &quot;audio/midi&quot;);
    }
    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(&quot;/sound/b_main.mid&quot;),
                                    &quot;audio/midi&quot;);
      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




 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons