📄 soundcontrol.java
字号:
/* * JCollapse - Java Collapse Game * Copyright (C) 2005 Erico Gon鏰lves Rimoli * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */package sourceforge.net.projects.jcollapse.game.components;import java.util.ArrayList;import sourceforge.net.projects.jcollapse.engine.sound.PlayList;import sourceforge.net.projects.jcollapse.engine.sound.Sound;import sourceforge.net.projects.jcollapse.engine.sound.SoundException;/** * With this class, you can control sound (initialization, events, etc...) * @author erico */public class SoundControl { private PlayList m_PlayList; private ArrayList<SoundControlListener> m_SoundControlListener; private boolean m_Enabled = false; private boolean m_AllReady = false; /** * Sound events * @author erico */ public static enum SoundEvent { PLAY, PAUSE, RESUME, STOP }; /** * Default constructor */ public SoundControl() { m_PlayList = new PlayList(); m_SoundControlListener = new ArrayList<SoundControlListener>( 1 ); } /** * Add a new SoundControlListener that listen for events in this control * @param soundControlListener SoundControlListener object */ public void addSoundControlListener( SoundControlListener soundControlListener ) { m_SoundControlListener.add( soundControlListener ); } /** * Remove SoundControlListener that listen for events in this control * @param soundControlListener the SoundControlListener object * @return true if SoundControlListener is removed */ public boolean removeSoundControlListener( SoundControlListener soundControlListener ) { return m_SoundControlListener.remove( soundControlListener ); } /** * Add sound to your playlist * @param name the sound name that will be used to do sound events like PLAY * @param sound the sound object that can be contain a Wave or Midi */ public void add( String name, Sound sound ) { m_PlayList.addSound( name, sound ); m_AllReady = false; } /** * Initialize sound system * @return true if initialization process is ok */ public boolean initialize() { if( isAllReady() ) return true; try { m_PlayList.initialize(); m_Enabled = true; m_AllReady = true; return true; }catch ( SoundException ex1 ) { m_Enabled = false; m_AllReady = false; ex1.printStackTrace(); System.out.println( "\nError initializing sound system: \n" + ex1.getMessage() ); return false; } finally { fireSoundStateChanged( true, m_Enabled, m_AllReady ); } } /** * Return true if all sound resources are initialized ready to be used * @return true if all sound resources are initialized ready to be used */ public boolean isAllReady() { return m_AllReady; } /** * All sound must be played/Stoped/Paused/Resumed by this method * @param soundName the Sound name * @param se the name of SoundEvent */ public void doEvent( String soundName, SoundEvent se ) { //test if user enabled sound and Sound is initialized if( !isEnabled() || !m_PlayList.isInitialized( soundName ) ) return; if( se == SoundEvent.PLAY ) m_PlayList.play( soundName ); else if( se == SoundEvent.STOP ) m_PlayList.stop( soundName ); else if( se == SoundEvent.PAUSE ) m_PlayList.pause( soundName ); else if( se == SoundEvent.RESUME ) m_PlayList.resume( soundName ); } /** * Set enabled or not the sound * @param enabled turn sound on/off */ public void setEnabled( boolean enabled ) { if( m_Enabled != enabled ) { m_Enabled = enabled; if( !m_Enabled ) stopAll(); fireSoundStateChanged( false, m_Enabled, m_AllReady ); } } /** * Sound is enabled or not state can be retrived by this method * @return true if sound control is enabled */ public boolean isEnabled() { return m_Enabled; } /** * Terminate sound events and release resources */ public void terminate() { stopAll(); m_PlayList.release(); m_AllReady = false; m_Enabled = false; System.out.println( "Sound system disabled" ); } private void stopAll() { for( Sound s : m_PlayList.getSoundCollection() ) if( s.isInitialized() ) s.stop(); } private void fireSoundStateChanged( boolean initializing, boolean enabled, boolean allReady ) { for( SoundControlListener scl : m_SoundControlListener ) scl.soundStateChanged( ( initializing ? SoundControlListener.INITIALIZING : 0 ) + ( enabled ? SoundControlListener.ENABLED : 0 ) + ( allReady ? SoundControlListener.ALLREADY : 0 ) ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -