📄 midi.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 javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
public class Midi extends Sound {
private Sequencer m_Sequencer;
private boolean m_Loop = false;
/**
* Constructor for Midi objects
* @param local the local of Midi resource
*/
public Midi( URL local ) {
this( local, false );
}
/**
* Constructor for Midi objects
* @param local the local of Midi resource
* @param loop if true, the sound play continuosly
*/
public Midi( URL local, boolean loop ) {
super( local );
setLoop( loop );
}
/* (non-Javadoc)
* @see sourceforge.net.projects.jcollapse.engine.sound.Playable#initialize()
*/
public void initialize() throws SoundException {
Sequence sequence;
//if Sequencer is ok, return
if( isInitialized() )
return;
try {
sequence = MidiSystem.getSequence( getLocal() );
m_Sequencer = MidiSystem.getSequencer();
m_Sequencer.open();
m_Sequencer.setSequence( sequence );
}catch( InvalidMidiDataException ex1 ) {
throw new SoundException( "Invalid midi data: " + getLocal().getFile(), ex1 );
}catch( MidiUnavailableException ex1 ) {
throw new SoundException( "Midi unavailable: " + getLocal().getFile(), ex1 );
}catch( IOException ex1 ) {
throw new SoundException( "IO error: : " + getLocal().getFile(), ex1 );
}
}
/* (non-Javadoc)
* @see sourceforge.net.projects.jcollapse.engine.sound.Sound#isInitialized()
*/
public boolean isInitialized() {
return( m_Sequencer != null && m_Sequencer.isOpen() );
}
/* (non-Javadoc)
* @see sourceforge.net.projects.jcollapse.engine.sound.Playable#play()
*/
public void play() {
m_Sequencer.setLoopCount( ( m_Loop ? -1 : 0 ) );
m_Sequencer.setTickPosition( 0 );
m_Sequencer.start();
}
/* (non-Javadoc)
* @see sourceforge.net.projects.jcollapse.engine.sound.Playable#pause()
*/
public void pause() {
m_Sequencer.stop();
}
/* (non-Javadoc)
* @see sourceforge.net.projects.jcollapse.engine.sound.Playable#dispause()
*/
public void resume() {
m_Sequencer.start();
}
/* (non-Javadoc)
* @see sourceforge.net.projects.jcollapse.engine.sound.Playable#stop()
*/
public void stop() {
m_Sequencer.stop();
m_Sequencer.setTickPosition( 0 );
}
/* (non-Javadoc)
* @see sourceforge.net.projects.jcollapse.engine.sound.Playable#release()
*/
public void release() {
//if not initialized, return
if( !isInitialized() )
return;
stop();
m_Sequencer.close();
m_Sequencer = 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_Sequencer.setLoopCount( Sequencer.LOOP_CONTINUOUSLY );
else
m_Sequencer.setLoopCount( 0 );
}
}
/* (non-Javadoc)
* @see sourceforge.net.projects.jcollapse.engine.sound.Playable#getSoundType()
*/
public int getSoundType() {
return Playable.TYPE_MIDI;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -