⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysound.java

📁 j2me简单实例,j2me教程加源码,希望大家喜欢
💻 JAVA
字号:
package com.j2medev.chapter5;

/**
 *声音播放类
 *作者:孙宇哲
 *已经支持的机型有:
 *SE_K700,NOKIA_S60_V2,NOKIA_S60_V1
 *MOTO_V600,MOTO_E398,SAMSUNG_D508
 *
 *更新日期:2006-5-15
 *
 *更新日期:2006-5-25
 *增加对moto_e680的声音支持
 *
 */


//#if ( NOKIA_S40_V1 || NOKIA_S40_V2 )
//# import com.nokia.mid.sound.*;
//#
//#else
import javax.microedition.media.Player;				// Sun bug fix
import javax.microedition.media.Manager;			// compiler problem
import javax.microedition.media.control.VolumeControl ;
//#endif
import java.io.*;


///////////////////////
//	SFX
///////////////////////
public class MySound implements MyGameInterface {
    
    public int m_soundLevel ;
    public int m_currentSound;
    public int m_errorCode;
    public boolean bSoundOpen;
//#if ( NOKIA_S40_V1 || NOKIA_S40_V2 )
//#     public Sound[] m_soundPlayers ;
    //#else
    public Player[] m_soundPlayers ;
    public VolumeControl[] m_volumeControls ;
    //#endif
    
    //----------------------
    //	SFX
    //
    //	Constructor
    //----------------------
    
    public MySound() {
        
        
//#if ( NOKIA_S40_V1 || NOKIA_S40_V2 )
//#   m_soundPlayers=new Sound[SOUND_NAMES.length];
//# InputStream is = null;
//#
//#            for(int i=0;i<m_soundPlayers.length; i++)
//#         {
//#             try
//#             {
//#                 m_soundPlayers[i] = new Sound(readDataFromJar(RES_PATH+SOUND_NAMES[i]), Sound.FORMAT_TONE);
//#             }catch(Exception e){e.printStackTrace();}
//#         }
//#
//#else
        m_currentSound=-1;
        m_soundPlayers = new Player[SOUND_NAMES.length];
        m_volumeControls= new VolumeControl[SOUND_NAMES.length];
        
        InputStream is = null;
        for(int i=0;i<m_soundPlayers.length; i++) {
            try {
                is = getClass().getResourceAsStream(RES_PATH+SOUND_NAMES[i]);
                m_soundPlayers[i] = Manager.createPlayer(is, "audio/midi");
                
                m_soundPlayers[i].realize();
                
//#if (SAMSUNG_D508)
//#              m_soundPlayers[i].prefetch();
//#endif
                m_volumeControls[i] = (VolumeControl)m_soundPlayers[i].getControl("VolumeControl");
                
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
//#endif
        //setSoundLevel(m_soundLevel);
        
    }
    
    public byte[] readDataFromJar(String name) throws IOException {
        ByteArrayOutputStream bout;
        InputStream in = getClass().getResourceAsStream(name);
        bout = new ByteArrayOutputStream();
        for (int ret = in.read(); ret >= 0; ret = in.read()) {
            bout.write(ret);
        }
        return bout.toByteArray();
    }
    public void stop() {
        
//#if (NOKIA_S40_V1||NOKIA_S40_V2)
//#           for(int i=0; i<m_soundPlayers.length; i++)
//#         {
//#                    try{
//#                       System.out.println("sound  "+ i+ " "+ getSoundState(i));
//#                           if(getSoundState(i)==Sound.SOUND_PLAYING)
//#                           {
//#                            m_soundPlayers[i].stop();
//#                           System.out.println("sound  "+ i+ " "+ getSoundState(i));
//#                           }
//#                          System.out.println("sound  "+ i+ " "+ getSoundState(i));
//#
//#                    }catch(Exception e){
//#                    e.printStackTrace();
//#                    }
//#                   }
//#
//#else
        for(int i=0; i<m_soundPlayers.length; i++) {
            try{
//#if (MOTO_E680_H_DEVICE||MOTO_E680_H||MOTO_E680_S)
//#                            if( m_soundPlayers[i]!=null)
//#                            {
//#                                m_soundPlayers[i].stop();
//#                                releaseSound(i);
//#                             }
//#                    }
//#else
                if(getSoundState(i)==Player.STARTED) {
                    if( m_soundPlayers[i]!=null)
                        m_soundPlayers[i].stop();
                    
                }
                
                
                //#if (MOTO_E398||MOTO_V600 )
//#                         m_soundPlayers[i].deallocate();
                //#endif
            }
//#endif
            
            catch(Exception e){
                m_errorCode=2;
                e.printStackTrace();
            }
        }
//#endif
    }
    
    
    public void play(int tone) {
        if(!bSoundOpen) return;
        try{
            if(tone >=SOUND_NAMES.length)return;
            
            stop();
//#if (MOTO_E680_H_DEVICE||MOTO_E680_H||MOTO_E680_S)
//#
//#            reloadSound(tone)   ;
//#endif
            
            m_currentSound=tone;
            
//#if (NOKIA_S40_V1||NOKIA_S40_V2)
//#  try {
//#        m_soundPlayers[tone].play(1);
//#      }
//#      catch (Exception e) {}
//#
//#else
            //#if (SE_K700 ||  MOTO_V600 || MOTO_E398||SE_S700||MOTO_E680_H_DEVICE||MOTO_E680_H||MOTO_E680_S)
//# 
//# 
            //#elif (SAMSUNG_D508)
//#               if(getSoundState(tone)==Player.REALIZED)
//#               {
//#                     m_soundPlayers[tone].start();
//#                     stop();
//#               }
//#
//#
            //#else
            m_soundPlayers[tone].realize();
            m_soundPlayers[tone].prefetch();
            m_soundPlayers[tone].setMediaTime(0);
            //#endif
            
            m_volumeControls[tone].setLevel(m_soundLevel);
            m_soundPlayers[tone].start();
//#endif
            
            
        }catch(Exception e){
            m_errorCode=1;
            e.printStackTrace();
            
        }
        
    }
    public void setSoundLevel(int level) {
//#if (NOKIA_S40_V1||NOKIA_S40_V2)
//#
//#
//#else
        this.m_soundLevel = level ;
        
        for(int i=0; i<SOUND_NAMES.length; i++) {
            
            m_volumeControls[i].setLevel(level);
        }
//#endif
    }
    
    public void setSoundLoopCount(int soundIndex,int loopCount) {
//#if (NOKIA_S40_V1||NOKIA_S40_V2)
//#
//#
//#else
        m_soundPlayers[soundIndex].setLoopCount(loopCount);
//#endif
        
    }
    public int getSoundState(int soundIndex) {
        
        if(m_soundPlayers[soundIndex]!=null)
            return m_soundPlayers[soundIndex].getState();
        else return 0;
        
        
    }
    /**
     * 释放资源。
     */
    public void releaseSound(int i) {
        if(m_soundPlayers[i] != null) {
            m_soundPlayers[i].close();
            m_soundPlayers[i] = null;
        }
    }
    public void reloadSound(int i) {
        InputStream is = null;
        
        try {
            is = getClass().getResourceAsStream(RES_PATH+SOUND_NAMES[i]);
            m_soundPlayers[i] = Manager.createPlayer(is, "audio/midi");
            
//#if (MOTO_E680_H_DEVICE||MOTO_E680_H||MOTO_E680_S)
//#               m_soundPlayers[i].realize();
//#               m_soundPlayers[i].prefetch();
//#else
            m_soundPlayers[i].realize();
//#endif
//#if (SAMSUNG_D508)
//#              m_soundPlayers[i].prefetch();
//#endif
            m_volumeControls[i] = (VolumeControl)m_soundPlayers[i].getControl("VolumeControl");
            m_volumeControls[i].setLevel(m_soundLevel);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    public void setSoundOpen(boolean soundOpen) {
        this.bSoundOpen = soundOpen;
    }
}	// END SFX CLASS

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -