nokia播放声音

 static byte[] readSoundData(String name) {
   
     ByteArrayOutputStream out;
     InputStream in;
     byte[] res = null;
     try {
       in = name.getClass().getResourceAsStream(name);
       out = new ByteArrayOutputStream();
       for (int ret = in.read(); ret >= 0; ret = in.read())
           out.write(ret);
       res = out.toByteArray();
   
     } catch (Exception e) {
           e.printStackTrace();
     }
     
     return res;
  }
 
static void soundLoad(int soundID) {
      try {
          //ByteArrayInputStream bis = null;
          //DataInputStream dis = new DataInputStream(FILE_NAME_SOUNDS.getClass().getResourceAsStream(FILE_NAME_SOUNDS));
       //DataInputStream dis = new DataInputStream(FILE_NAME_SOUNDS.getClass().getResourceAsStream(FILE_NAME_SOUNDS)); 
       int sndCount = 5;
       int temp;
     s_soundData = new byte[sndCount][];
         //sndCount = dis.read();

          //s_sounds = new Player[sndCount];

          //for (int i = 0; i < sndCount; i++) {
              //int size = readNumber(dis, 2);
        if(/*i == 0 && */soundID == 0){
         temp = readSoundData("/AlexandraLedermann/sfx_good_jump.wav").length;
         s_soundData[soundID] = new byte[temp];
         s_soundData[soundID] = readSoundData("/AlexandraLedermann/sfx_good_jump.wav");
        }
        else if(/*i == 1 && */soundID == 1){ //unreached here
         temp = readSoundData("/AlexandraLedermann/m_townmap.wav").length;
         s_soundData[soundID] = new byte[temp];
         s_soundData[soundID] = readSoundData("/AlexandraLedermann/m_townmap.wav");
        }
        else if(/*i == 2 && */soundID == 2){
         temp = readSoundData("/AlexandraLedermann/m_congratulation.wav").length;
         s_soundData[soundID] = new byte[temp];
         s_soundData[soundID] = readSoundData("/AlexandraLedermann/m_congratulation.wav");
        }
        else if(/*i == 3 && */soundID == 3){ //unreached here
         temp = readSoundData("/AlexandraLedermann/m_failure.wav").length;
         s_soundData[soundID] = new byte[temp];
         s_soundData[soundID] = readSoundData("/AlexandraLedermann/m_failure.wav");
        }
        else if(/*i == 4 && */soundID == 4){
         temp = readSoundData("/AlexandraLedermann/m_title.wav").length;
         s_soundData[soundID] = new byte[temp];
         s_soundData[soundID] = readSoundData("/AlexandraLedermann/m_title.wav");
        }else{
            s_soundData[soundID] = null;
        }             

      }
      catch (Exception e) {
          {
              e.printStackTrace();
          }
      }
      System.gc();

  }
 
static void soundRelease() {
      if (s_currentSound != null)
   {
       s_currentSound.stop();
       s_currentSound.release();
    s_currentSound = null;
   }
   if (s_soundData != null)
      {
          for (int i = 0;i < s_soundData.length; i++ )
          {
              if (s_soundData[i] != null)
     {
                  s_soundData[i] = null;
     }
          }
          s_soundData = null;         
      }
   isSoundLoaded = false;
      System.gc();

  }
  static void Snd_setChannelVolume(int channel, int volume) throws Exception
  {
   try
   {
    if(s_currentSound != null)
     s_currentSound.setGain(150);
   }
   catch (Exception e)
   {
    s_currentSound = null;
   }
  }
  static void Snd_prepare(int index, int channel) throws Exception
  {
   try
   {
    if(s_currentSound == null)
     s_currentSound = new Sound(s_soundData[index], Sound.FORMAT_WAV));// if wav format
     //s_currentSound = new Sound(s_soundData[index], Sound.FORMAT_TONE));// if midi, ott format  
   }
   catch (Exception e)
   {
    e.printStackTrace();
    s_currentSound = null;
   }
  } 
  public static void soundPlay(int soundID, int loop) {
      if ( (!s_soundEnable) )
   {
        return;
   } 
   try {
            if (  (s_currentSound != null) && (s_currentSoundId == soundID)
                &&  (s_currentSound.getState() == Sound.SOUND_PLAYING)               
    )
                return;
   if (s_currentSoundId > 0)
       soundStop(s_currentSoundId);
            if ( s_currentSound != null)
   {
             s_currentSound.stop();
       s_currentSound.release();
    s_currentSound = null;
   }
   s_currentSoundId = soundID;
   s_isPlayingID = soundID;

       if (s_soundData[soundID] == null)
    {
        soundLoad(soundID);
    }

             if ((s_currentSound != null) && (s_currentSound.getState() == Sound.SOUND_PLAYING))
              return ;                       

    // Create player
    Snd_prepare(soundID, 0);
   
    // Set volume
    Snd_setChannelVolume(0, 0);
    //play   
    s_currentSound.play(loop);

   }      
        catch (Exception e) {
              e.printStackTrace();
  }

  }

  public static void soundStop(int id) {
      try {
          if (CRes.bDebugMode) {
              System.out.println("soundStop : " + id);
          }
          if (s_currentSound == null) {
              return;
          }
          // stop all sounds
          if (id == -1) {             
                  if (s_currentSound != null) {
                   s_currentSound.stop();
                   s_currentSound.release();
                   s_currentSound = null;        
          }             
          }
          // stop the specific sound
          else {
              if (s_currentSound != null) {
               s_currentSound.stop();
               s_currentSound.release();
               s_currentSound = null;

              }
          }
    //s_currentSound = null;
    //s_isPlayingID = -1;
    System.gc();
      }
      catch (Exception ex) {
              ex.printStackTrace();
      }
  } 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值