📄 wave.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.engine.sound;import java.io.IOException;import java.net.URL;import java.util.concurrent.atomic.AtomicBoolean;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.Clip;import javax.sound.sampled.DataLine;import javax.sound.sampled.LineUnavailableException;import javax.sound.sampled.SourceDataLine;import javax.sound.sampled.UnsupportedAudioFileException;public class Wave extends Sound implements Runnable { private Clip m_Clip; private boolean m_Loop = false; private boolean m_Terminate = false; private AtomicBoolean m_Play = new AtomicBoolean( false ); private Thread m_ThreadWave; /** * Constructor for Wave objects * @param local the local of Wave resource */ public Wave( URL local ) { this( local, false ); } /** * Constructor for Wave objects * @param local the local of Wave resource * @param loop if true, the sound play continuosly */ public Wave( URL local, boolean loop ) { super( local ); setLoop( loop ); } /* (non-Javadoc) * @see sourceforge.net.projects.jcollapse.engine.sound.Playable#initialize() */ public void initialize() throws SoundException { //if Clip is ok, return if( isInitialized() ) return; m_Terminate = false; AudioInputStream stream; SourceDataLine.Info info; try { stream = AudioSystem.getAudioInputStream( getLocal() ); //ALAW and ULAW encodings must be converted //to PCM_SIGNED before it can be played /* if( stream.getFormat().getEncoding() != AudioFormat.Encoding.PCM_SIGNED ) { stream = AudioSystem.getAudioInputStream( new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, stream.getFormat().getSampleRate(), stream.getFormat().getSampleSizeInBits(), stream.getFormat().getChannels(), stream.getFormat().getChannels() * ( stream.getFormat().getSampleSizeInBits() / 8 ), stream.getFormat().getFrameRate(), true ), stream ); System.out.println( "Audio converted to PCM_SIGNED: " + getLocal().getPath() ); }*/ info = new DataLine.Info( Clip.class, stream.getFormat(), ( (int)stream.getFrameLength() * stream.getFormat().getFrameSize() ) ); m_Clip = (Clip)AudioSystem.getLine( info ); m_Clip.open( stream ); //FloatControl gainControl = (FloatControl)m_Clip.getControl( FloatControl.Type.MASTER_GAIN ); //gainControl.setValue( (float)( Math.log( 1D ) / Math.log( 10F ) * 20F ) ); m_ThreadWave = new Thread( this, "Wave_" + getLocal().getFile() ); m_ThreadWave.setDaemon( true ); m_ThreadWave.start(); }catch( UnsupportedAudioFileException ex1 ) { throw new SoundException( "Unsupported audio. File: " + getLocal().getFile(), ex1 ); }catch( IOException ex1 ) { throw new SoundException( "Read error. File: " + getLocal().getFile(), ex1 ); }catch( LineUnavailableException ex1 ) { throw new SoundException( "Line unavailable. File: " + getLocal().getFile(), ex1 ); }catch( Exception ex1 ) { throw new SoundException( "Unknown error. File: " + getLocal().getFile(), ex1 ); } } /* (non-Javadoc) * @see sourceforge.net.projects.jcollapse.engine.sound.Sound#isInitialized() */ public boolean isInitialized() { return ( m_ThreadWave != null && m_ThreadWave.isAlive() ); } /* (non-Javadoc) * @see sourceforge.net.projects.jcollapse.engine.sound.Playable#play() */ public void play() { m_Clip.setLoopPoints( 0, ( m_Loop ? -1 : 0 ) ); m_Clip.setFramePosition( 0 ); m_Play.compareAndSet( false, true ); } /* (non-Javadoc) * @see sourceforge.net.projects.jcollapse.engine.sound.Playable#pause() */ public void pause() { m_Clip.stop(); m_Play.compareAndSet( true, false ); } /* (non-Javadoc) * @see sourceforge.net.projects.jcollapse.engine.sound.Playable#dispause() */ public void resume() { m_Play.compareAndSet( false, true ); } /* (non-Javadoc) * @see sourceforge.net.projects.jcollapse.engine.sound.Playable#stop() */ public void stop() { m_Play.compareAndSet( true, false ); m_Clip.stop(); } /* (non-Javadoc) * @see sourceforge.net.projects.jcollapse.engine.sound.Playable#release() */ public void release() { //cause thread to end m_Terminate = true; //wait thread (death) while( m_ThreadWave.isAlive() ) Thread.yield(); stop(); m_Clip = null; m_ThreadWave = null; } /* (non-Javadoc) * @see sourceforge.net.projects.jcollapse.engine.sound.Playable#setLoop(boolean) */ public void setLoop( boolean loop ) { m_Loop = loop; if( isInitialized() ) { if( loop ) m_Clip.setLoopPoints( 0, -1 ); else m_Clip.setLoopPoints( 0, 0 ); } } public void run() { while( !m_Terminate ) { if( m_Play.get() ) m_Clip.start(); try { Thread.sleep( 15 ); }catch( InterruptedException ex1 ) {} } //System.out.println( "Thread terminated: " + Thread.currentThread().getName() ); } /* (non-Javadoc) * @see sourceforge.net.projects.jcollapse.engine.sound.Playable#getSoundType() */ public int getSoundType() { return TYPE_WAVE; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -