📄 soundplayer.java
字号:
Clip clip = (Clip) currentSound;
/** play the clip */
clip.start();
try
{
thread.sleep(99);
}
catch (Exception e)
{ }
/** iterate */
while ((paused || clip.isActive()) && thread != null && !bump)
{
try
{
thread.sleep(99);
}
catch (Exception e)
{
break;
}
}
/** stop playing */
clip.stop();
/** close the clip */
clip.close();
}
currentSound = null;
}
/**
* Must have method.
* @param event line event
*/
public void update(LineEvent event)
{
if (event.getType() == LineEvent.Type.STOP && !paused)
{
audioEOM = true;
}
}
/**
* Must have method.
* @param message meta message
*/
public void meta(MetaMessage message)
{
/** 47 is end of track */
if (message.getType() == 47)
{
midiEOM = true;
}
}
/**
* @return current thread
*/
public Thread getThread()
{
return thread;
}
/**
* Creates the thread and start it.
*/
public void start()
{
/** create the thread */
thread = new Thread(this);
/** name the thread */
thread.setName("SoundPlayer");
/** start the thread */
thread.start();
}
/**
* Stops then destroys the thread.
*/
public void stop()
{
/** if thread is alive */
if (thread != null)
{
/** destroy it */
thread.interrupt();
}
/** set it to null */
thread = null;
}
/**
* Thread method.
*/
public void run() {
/** iterate */
do
{
/** if the sound object exists */
if( loadSound(sounds.elementAt(num)) == true )
{
/** play the sound */
playSound();
}
}
/** while loop is on and the thread isn't killed */
while (loop && thread != null);
/** if the thread isn't dead */
if (thread != null)
{
/** kill it */
startB.doClick();
}
/** reste everything */
thread = null;
currentName = null;
currentSound = null;
}
/**
* Set pan.
*/
public void setPan() {
/** default value of 0: pan left / right evenly */
int value = 0;
/** if the sound object is a clip */
if (currentSound instanceof Clip)
{
try
{
/** get the clip */
Clip clip = (Clip) currentSound;
/** get the current pane value */
FloatControl panControl =
(FloatControl) clip.getControl(FloatControl.Type.PAN);
/** set the pan value */
panControl.setValue(value/100.0f);
}
catch (Exception ex)
{
}
}
/** if the sound object is a sequence */
else if (currentSound instanceof Sequence ||
currentSound instanceof BufferedInputStream)
{
/** iterate through all the channels */
for (int i = 0; i < channels.length; i++)
{
/** and set the pan values */
channels[i].controlChange(10,
(int)(((double)value + 100.0) / 200.0 * 127.0));
}
}
}
/**
* Set gain (volumn).
* @param value volumn value
*/
public void setGain(double value)
{
/** if current sound object is a clip */
if (currentSound instanceof Clip)
{
try
{
/** get the clip */
Clip clip = (Clip) currentSound;
/** get the current gain */
FloatControl gainControl =
(FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
/** create and calculate the gain value */
float dB = (float)
(Math.log(value==0.0?0.0001:value)/Math.log(10.0)*20.0);
/** set the gain value */
gainControl.setValue(dB);
}
catch (Exception ex)
{
}
}
/** if current sound object is a sequence */
else if (currentSound instanceof Sequence ||
currentSound instanceof BufferedInputStream)
{
/** iterate through all the channels */
for (int i = 0; i < channels.length; i++)
{
/** set gain */
channels[i].controlChange(7, (int)(value * 127.0));
}
}
}
/**
* Set gain to 0.
*/
public void mute()
{
volumn = 0;
setGain(volumn);
/** pause the sound for a mili second */
bump = true;
}
/**
* Set gainto 100.
*/
public void unmute()
{
volumn = 100;
setGain(volumn);
/** pause the sound for a mili second */
bump = true;
}
/**
* Change to different sound.
* @param n new sound offset
* @param l loop or not
*/
public void change(int n, boolean l)
{
paused = false;
/** change text of pause button */
pauseB.setText("Pause");
/** get loop and offset */
loop = l;
num = n;
/** pause the sound for a mili second */
bump = true;
/** if no sound is playing */
if (startB.getText().equals("Start"))
/** play the sound */
startB.doClick();
}
/**
* Play the current sound.
*/
public void controlPlay()
{
startB.doClick();
}
/**
* Stop the current sound.
*/
public void controlStop()
{
startB.doClick();
}
/**
* Play the previous sound.
*/
public void controlBack()
{
prevB.doClick();
}
/**
* Play the next sound.
*/
public void controlNext()
{
nextB.doClick();
}
/**
* Set buttons states.
* @param state enabl or disable
*/
public void setComponentsEnabled(boolean state)
{
pauseB.setEnabled(state);
prevB.setEnabled(state);
nextB.setEnabled(state);
}
/**
* @return whether something is playing or not
*/
public boolean isPlaying() {
return (startB.getText().equals("Start") ? false : true);
}
/**
* Action listener.
* @param e action event
*/
public void actionPerformed(ActionEvent e)
{
/** get source of event */
JButton button = (JButton) e.getSource();
/** if the start button is clicked */
if (button.getText().equals("Start"))
{
/** not paused */
paused = false;
/** if offset is out of range than set it to 0 */
num = num == -1 ? 0 : num;
/** start playing */
start();
/** change button text to "Stop" */
button.setText("Stop");
/** change buttons states */
setComponentsEnabled(true);
}
/** if the stop button is clicked */
else if (button.getText().equals("Stop"))
{
/** not paused */
paused = false;
/** stop playing */
stop();
/** change texts of buttons */
button.setText("Start");
pauseB.setText("Pause");
/** change buttons states */
setComponentsEnabled(false);
}
/** if the pause button is clicked */
else if (button.getText().equals("Pause"))
{
/** paused */
paused = true;
/** if the sound object is a clip */
if (currentSound instanceof Clip)
{
/** stop the clip */
((Clip) currentSound).stop();
}
/** if the sound object is a sequence */
else if (currentSound instanceof Sequence ||
currentSound instanceof BufferedInputStream)
{
/** stop the sequence */
sequencer.stop();
}
/** change the button text */
pauseB.setText("Resume");
}
else if (button.getText().equals("Resume"))
{
/** not paused anymore */
paused = false;
/** if sound is a clip */
if (currentSound instanceof Clip)
{
/** start the clip */
((Clip) currentSound).start();
}
/** if sound is a sequence */
else if (currentSound instanceof Sequence ||
currentSound instanceof BufferedInputStream)
{
/** start the sequence */
sequencer.start();
}
/** change the button text */
pauseB.setText("Pause");
}
/** if the back button is clicked */
else if (button.getText().equals("<<"))
{
paused = false;
/** change button text */
pauseB.setText("Pause");
/** go to previous sound */
num = num-1 < 0 ? sounds.size()-1 : num-2;
/** change sound */
bump = true;
}
/** if the next button is clicked */
else if (button.getText().equals(">>"))
{
paused = false;
/** change button text */
pauseB.setText("Pause");
/** go to next sound */
num = num+1 == sounds.size() ? -1 : num;
/** change sound */
bump = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -