科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道详细介绍手机游戏中的声音处理

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

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

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

作者:中国IT实验室 来源:中国IT实验室 2007年9月22日

关键字: 声音 游戏

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共3页)

  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;
  }

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章