下面是代码,哪位大虾能帮我把下面的代码添加2个功能:
1、点击一首歌曲,播放完,停顿30秒后继续播放该首音乐
——“循环单曲播放”背景音乐
2、点击循环播放,3首背景音乐循环“随机”播放,同时播放完一首后,停顿30秒后随机播放下首音乐
——“循环随机播放”背景音乐
注: 请不要其他转载代码,发的太多,又要从代码中一个一个的总结提取,我看得头都快大了,好不容总结出了下面的代码,修改下面的代码即可,谢谢!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import java.net.*;
import javax.sound.midi.spi.MidiFileReader;
public class Music extends JFrame implements Runnable {
private JMenuBar menuBar = new JMenuBar();
private JMenu mMusic=new JMenu("音乐");
private JCheckBoxMenuItem
mMusic0 = new JCheckBoxMenuItem("m1"),
mMusic1 = new JCheckBoxMenuItem("m2"),
mMusic2 = new JCheckBoxMenuItem("m3"),
mLoop = new JCheckBoxMenuItem("循环播放", true),
mStop=new JCheckBoxMenuItem("停止播放");
private Sequencer sequencer;
Sequence sequence;
URL urlMusic0,urlMusic1,urlMusic2;
MidiFileReader midifilereader;
boolean loop; //是否循环播放
Music(){
menuBar.add(mMusic);
mMusic.add(mMusic0);
mMusic.add(mMusic1);
mMusic.add(mMusic2);
mMusic.addSeparator();
mMusic.add(mLoop);
mMusic.add(mStop);
setJMenuBar(menuBar);
setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getSize().width) / 2,
(Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height) / 2);
urlMusic0= getClass().getResource("sound/music0.mid");
urlMusic1= getClass().getResource("sound/music1.mid");
urlMusic2= getClass().getResource("sound/music2.mid");
try{ sequencer= MidiSystem.getSequencer(false);
sequencer.open();
}catch(Exception e){
e.printStackTrace();
}
mMusic0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(true);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(false);
loop=false;
playMusic0();
}
});
mMusic1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(true);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(false);
loop=false;
playMusic1();
}
});
mMusic2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(true);
mLoop.setState(false);
mStop.setState(false);
loop=false;
playMusic2();
}
});
mLoop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(true);
mStop.setState(false);
playMusicLoop();
}
});
mStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(true);
playMusicStop();
}
});
setSize(300, 200); //设置窗口大小
setVisible(true); //设置窗口为可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void playMusic0(){
try{
sequencer.stop();
sequence= MidiSystem.getSequence(urlMusic0);
sequencer.setSequence(sequence);
sequencer.start();
}catch(Exception e){
e.printStackTrace();
}
}
public void playMusic1(){
try{
sequencer.stop();
sequence= MidiSystem.getSequence(urlMusic1);
sequencer.setSequence(sequence);
sequencer.start();
}catch(Exception e){
e.printStackTrace();
}
}
public void playMusic2(){
try{
sequencer.stop();
sequence= MidiSystem.getSequence(urlMusic2);
sequencer.setSequence(sequence);
sequencer.start();
}catch(Exception e){
e.printStackTrace();
}
}
public void playMusicStop(){
loop=false;
sequencer.stop();
}
public void playMusicLoop(){
loop=true;
}
public void run(){
/* while(loop){
try{
Thread.sleep(30000);
}catch(Exception e){
e.printStackTrace();
}
}
*/
}
public static void main(String args[]) {
new Music();
}
}
6 个解决方案
#1
哪位大虾能帮我把上面的代码添加个循环播放功能啊。。。
#2
loopButton=new JButton("循环");
loopButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
audioPlay.loop();//循环方法
playButton.setEnabled(false);
loopButton.setEnabled(false);
stopButton.setEnabled(true);
combFileName.setEnabled(false);
}
});
#3
up
#4
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import java.net.*;
import javax.sound.midi.spi.MidiFileReader;
public class Music extends JFrame {
private JMenuBar menuBar = new JMenuBar();
private JMenu mMusic=new JMenu("音乐");
private JCheckBoxMenuItem
mMusic0 = new JCheckBoxMenuItem("m1"),
mMusic1 = new JCheckBoxMenuItem("m2"),
mMusic2 = new JCheckBoxMenuItem("m3"),
mLoop = new JCheckBoxMenuItem("循环播放"),
mStop=new JCheckBoxMenuItem("停止播放");
private Sequencer sequencer;
private Sequence sequence;
private URL urlMusic0,urlMusic1,urlMusic2;
boolean loopAll=true; //判断是全部循环播放,还是单曲循环播放
boolean play=false;
private int num;
private Thread thread;
Music(){
menuBar.add(mMusic);
mMusic.add(mMusic0);
mMusic.add(mMusic1);
mMusic.add(mMusic2);
mMusic.addSeparator();
mMusic.add(mLoop);
mMusic.add(mStop);
setJMenuBar(menuBar);
setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getSize().width) / 2,
(Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height) / 2);
urlMusic0= getClass().getResource("sound/music0.mid");
urlMusic1= getClass().getResource("sound/music1.mid");
urlMusic2= getClass().getResource("sound/music2.mid");
try{ sequencer= MidiSystem.getSequencer(false);
sequencer.open();
}catch(Exception e){
e.printStackTrace();
}
thread=new Thread(new MusicRun());
thread.start();
mMusic0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(true);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(false);
setMusic(0);
}
});
mMusic1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(true);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(false);
setMusic(1);
}
});
mMusic2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(true);
mLoop.setState(false);
mStop.setState(false);
setMusic(2);
}
});
mLoop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(true);
mStop.setState(false);
setMusicLoop();
}
});
mStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(true);
stopMusic();
}
});
setSize(300, 200); //设置窗口大小
setVisible(true); //设置窗口为可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void setMusic(int n){
sequencer.stop();
loopAll=false;
num=n;
play=true;
}
public void setMusicLoop(){
sequencer.stop();
loopAll=true;
play=true;
}
public void stopMusic(){
play=false;
sequencer.stop();
}
public void playMusic(){
play=true;
}
public class MusicRun implements Runnable {
public void run(){
while(true){
if(play){
if(loopAll){
try{
num=(int)(Math.random()*10)%3;
if(num==0){
sequence= MidiSystem.getSequence(urlMusic0);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(76000); //音乐时间+30秒的停顿时间
}else
if(num==1){
sequence= MidiSystem.getSequence(urlMusic1);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(140000);
}else{
sequence= MidiSystem.getSequence(urlMusic2);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(64000);
}
}catch(Exception e){
e.printStackTrace();
}
}else{
try{
if(num==0){
sequence= MidiSystem.getSequence(urlMusic0);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(76000);
}else
if(num==1){
sequence= MidiSystem.getSequence(urlMusic1);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(140000);
}else{
sequence= MidiSystem.getSequence(urlMusic2);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(64000);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
try{
Thread.sleep(2000); //每2秒对线程进行检测,是否播放音乐
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public static void main(String args[]) {
new Music();
}
}
按你的代码进行了修改,有你说的2个功能:
1、点击一首歌曲,播放完,停顿30秒后继续播放该首音乐
——“循环单曲播放”背景音乐
2、点击循环播放,3首背景音乐循环“随机”播放,同时播放完一首后,停顿30秒后随机播放下首音乐
——“循环随机播放”背景音乐 ”
改动很大,虽然都有你说的那2个功能,不过有个缺点:当正在播放的时候,换成其他音乐,会有个播放延时问题,例如 位置Thread.sleep(64000); 需要等待64秒(音乐本身的时间+30秒=64秒)后再播放其他音乐,如果在播放时,改变播放模式或音乐,会停止播放当前音乐,但要等待64秒后才会继续循环播放,这点问题就是在sleep();如何把sleep()的线程唤醒,然后播放其他音乐。好像sleep()不能被唤醒,使用wait(64000)可以,但循环出错,到此我是无能为力了,正待其他人解答吧。关注...
#5
太好了,谢谢4楼,可我怎么解决你说得问题啊。。。又是个头疼的事
#6
今天下午我问了我朋友,线程sleep是没有函数可以把它唤醒的,可以使用wait(),如果非要使用sleep的话,使用线程中断方法thread.interrupt(); 方法很简单,但会抛出异常
下面是可以运行的代码:
下面是可以运行的代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import java.net.*;
public class Music extends JFrame {
private JMenuBar menuBar = new JMenuBar();
private JMenu mMusic=new JMenu("音乐");
private JCheckBoxMenuItem
mMusic0 = new JCheckBoxMenuItem("m1"),
mMusic1 = new JCheckBoxMenuItem("m2"),
mMusic2 = new JCheckBoxMenuItem("m3"),
mLoop = new JCheckBoxMenuItem("循环播放"),
mStop=new JCheckBoxMenuItem("停止播放");
private Sequencer sequencer;
private Sequence sequence;
private URL urlMusic0,urlMusic1,urlMusic2;
boolean loopAll=true; //判断是全部循环播放,还是单曲循环播放
boolean play=false;
private int num;
private Thread thread;
Music(){
menuBar.add(mMusic);
mMusic.add(mMusic0);
mMusic.add(mMusic1);
mMusic.add(mMusic2);
mMusic.addSeparator();
mMusic.add(mLoop);
mMusic.add(mStop);
setJMenuBar(menuBar);
setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getSize().width) / 2,
(Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height) / 2);
urlMusic0= getClass().getResource("sound/music0.mid");
urlMusic1= getClass().getResource("sound/music1.mid");
urlMusic2= getClass().getResource("sound/music2.mid");
try{ sequencer= MidiSystem.getSequencer(false);
sequencer.open();
}catch(Exception e){
e.printStackTrace();
}
thread=new Thread(new MusicRun());
thread.start();
mMusic0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(true);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(false);
setMusic(0);
}
});
mMusic1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(true);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(false);
setMusic(1);
}
});
mMusic2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(true);
mLoop.setState(false);
mStop.setState(false);
setMusic(2);
}
});
mLoop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(true);
mStop.setState(false);
setMusicLoop();
}
});
mStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(true);
stopMusic();
}
});
setSize(300, 200); //设置窗口大小
setVisible(true); //设置窗口为可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void setMusic(int n){
sequencer.stop();
loopAll=false;
num=n;
play=true;
if(thread.getState()==Thread.State.TIMED_WAITING)
thread.interrupt();
}
public void setMusicLoop(){
sequencer.stop();
loopAll=true;
play=true;
if(thread.getState()==Thread.State.TIMED_WAITING)
thread.interrupt();
}
public void stopMusic(){
play=false;
sequencer.stop();
}
public void playMusic(){
play=true;
}
public class MusicRun extends Thread {
public void run(){
while(true){
if(play){
if(loopAll){
try{
num=(int)(Math.random()*10)%3;
if(num==0){
sequence= MidiSystem.getSequence(urlMusic0);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(76000);
}else
if(num==1){
sequence= MidiSystem.getSequence(urlMusic1);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(140000);
}else{
sequence= MidiSystem.getSequence(urlMusic2);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(64000);
}
}catch(Exception e){
}
}else{
try{
if(num==0){
sequence= MidiSystem.getSequence(urlMusic0);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(76000);
}else
if(num==1){
sequence= MidiSystem.getSequence(urlMusic1);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(140000);
}else{
sequence= MidiSystem.getSequence(urlMusic2);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(64000);
}
}catch(Exception e){
}
}
}
}
}
}
public static void main(String args[]) {
new Music();
}
}
#1
哪位大虾能帮我把上面的代码添加个循环播放功能啊。。。
#2
loopButton=new JButton("循环");
loopButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
audioPlay.loop();//循环方法
playButton.setEnabled(false);
loopButton.setEnabled(false);
stopButton.setEnabled(true);
combFileName.setEnabled(false);
}
});
#3
up
#4
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import java.net.*;
import javax.sound.midi.spi.MidiFileReader;
public class Music extends JFrame {
private JMenuBar menuBar = new JMenuBar();
private JMenu mMusic=new JMenu("音乐");
private JCheckBoxMenuItem
mMusic0 = new JCheckBoxMenuItem("m1"),
mMusic1 = new JCheckBoxMenuItem("m2"),
mMusic2 = new JCheckBoxMenuItem("m3"),
mLoop = new JCheckBoxMenuItem("循环播放"),
mStop=new JCheckBoxMenuItem("停止播放");
private Sequencer sequencer;
private Sequence sequence;
private URL urlMusic0,urlMusic1,urlMusic2;
boolean loopAll=true; //判断是全部循环播放,还是单曲循环播放
boolean play=false;
private int num;
private Thread thread;
Music(){
menuBar.add(mMusic);
mMusic.add(mMusic0);
mMusic.add(mMusic1);
mMusic.add(mMusic2);
mMusic.addSeparator();
mMusic.add(mLoop);
mMusic.add(mStop);
setJMenuBar(menuBar);
setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getSize().width) / 2,
(Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height) / 2);
urlMusic0= getClass().getResource("sound/music0.mid");
urlMusic1= getClass().getResource("sound/music1.mid");
urlMusic2= getClass().getResource("sound/music2.mid");
try{ sequencer= MidiSystem.getSequencer(false);
sequencer.open();
}catch(Exception e){
e.printStackTrace();
}
thread=new Thread(new MusicRun());
thread.start();
mMusic0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(true);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(false);
setMusic(0);
}
});
mMusic1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(true);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(false);
setMusic(1);
}
});
mMusic2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(true);
mLoop.setState(false);
mStop.setState(false);
setMusic(2);
}
});
mLoop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(true);
mStop.setState(false);
setMusicLoop();
}
});
mStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(true);
stopMusic();
}
});
setSize(300, 200); //设置窗口大小
setVisible(true); //设置窗口为可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void setMusic(int n){
sequencer.stop();
loopAll=false;
num=n;
play=true;
}
public void setMusicLoop(){
sequencer.stop();
loopAll=true;
play=true;
}
public void stopMusic(){
play=false;
sequencer.stop();
}
public void playMusic(){
play=true;
}
public class MusicRun implements Runnable {
public void run(){
while(true){
if(play){
if(loopAll){
try{
num=(int)(Math.random()*10)%3;
if(num==0){
sequence= MidiSystem.getSequence(urlMusic0);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(76000); //音乐时间+30秒的停顿时间
}else
if(num==1){
sequence= MidiSystem.getSequence(urlMusic1);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(140000);
}else{
sequence= MidiSystem.getSequence(urlMusic2);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(64000);
}
}catch(Exception e){
e.printStackTrace();
}
}else{
try{
if(num==0){
sequence= MidiSystem.getSequence(urlMusic0);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(76000);
}else
if(num==1){
sequence= MidiSystem.getSequence(urlMusic1);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(140000);
}else{
sequence= MidiSystem.getSequence(urlMusic2);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(64000);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
try{
Thread.sleep(2000); //每2秒对线程进行检测,是否播放音乐
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public static void main(String args[]) {
new Music();
}
}
按你的代码进行了修改,有你说的2个功能:
1、点击一首歌曲,播放完,停顿30秒后继续播放该首音乐
——“循环单曲播放”背景音乐
2、点击循环播放,3首背景音乐循环“随机”播放,同时播放完一首后,停顿30秒后随机播放下首音乐
——“循环随机播放”背景音乐 ”
改动很大,虽然都有你说的那2个功能,不过有个缺点:当正在播放的时候,换成其他音乐,会有个播放延时问题,例如 位置Thread.sleep(64000); 需要等待64秒(音乐本身的时间+30秒=64秒)后再播放其他音乐,如果在播放时,改变播放模式或音乐,会停止播放当前音乐,但要等待64秒后才会继续循环播放,这点问题就是在sleep();如何把sleep()的线程唤醒,然后播放其他音乐。好像sleep()不能被唤醒,使用wait(64000)可以,但循环出错,到此我是无能为力了,正待其他人解答吧。关注...
#5
太好了,谢谢4楼,可我怎么解决你说得问题啊。。。又是个头疼的事
#6
今天下午我问了我朋友,线程sleep是没有函数可以把它唤醒的,可以使用wait(),如果非要使用sleep的话,使用线程中断方法thread.interrupt(); 方法很简单,但会抛出异常
下面是可以运行的代码:
下面是可以运行的代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import java.net.*;
public class Music extends JFrame {
private JMenuBar menuBar = new JMenuBar();
private JMenu mMusic=new JMenu("音乐");
private JCheckBoxMenuItem
mMusic0 = new JCheckBoxMenuItem("m1"),
mMusic1 = new JCheckBoxMenuItem("m2"),
mMusic2 = new JCheckBoxMenuItem("m3"),
mLoop = new JCheckBoxMenuItem("循环播放"),
mStop=new JCheckBoxMenuItem("停止播放");
private Sequencer sequencer;
private Sequence sequence;
private URL urlMusic0,urlMusic1,urlMusic2;
boolean loopAll=true; //判断是全部循环播放,还是单曲循环播放
boolean play=false;
private int num;
private Thread thread;
Music(){
menuBar.add(mMusic);
mMusic.add(mMusic0);
mMusic.add(mMusic1);
mMusic.add(mMusic2);
mMusic.addSeparator();
mMusic.add(mLoop);
mMusic.add(mStop);
setJMenuBar(menuBar);
setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getSize().width) / 2,
(Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height) / 2);
urlMusic0= getClass().getResource("sound/music0.mid");
urlMusic1= getClass().getResource("sound/music1.mid");
urlMusic2= getClass().getResource("sound/music2.mid");
try{ sequencer= MidiSystem.getSequencer(false);
sequencer.open();
}catch(Exception e){
e.printStackTrace();
}
thread=new Thread(new MusicRun());
thread.start();
mMusic0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(true);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(false);
setMusic(0);
}
});
mMusic1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(true);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(false);
setMusic(1);
}
});
mMusic2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(true);
mLoop.setState(false);
mStop.setState(false);
setMusic(2);
}
});
mLoop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(true);
mStop.setState(false);
setMusicLoop();
}
});
mStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
mMusic0.setState(false);
mMusic1.setState(false);
mMusic2.setState(false);
mLoop.setState(false);
mStop.setState(true);
stopMusic();
}
});
setSize(300, 200); //设置窗口大小
setVisible(true); //设置窗口为可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void setMusic(int n){
sequencer.stop();
loopAll=false;
num=n;
play=true;
if(thread.getState()==Thread.State.TIMED_WAITING)
thread.interrupt();
}
public void setMusicLoop(){
sequencer.stop();
loopAll=true;
play=true;
if(thread.getState()==Thread.State.TIMED_WAITING)
thread.interrupt();
}
public void stopMusic(){
play=false;
sequencer.stop();
}
public void playMusic(){
play=true;
}
public class MusicRun extends Thread {
public void run(){
while(true){
if(play){
if(loopAll){
try{
num=(int)(Math.random()*10)%3;
if(num==0){
sequence= MidiSystem.getSequence(urlMusic0);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(76000);
}else
if(num==1){
sequence= MidiSystem.getSequence(urlMusic1);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(140000);
}else{
sequence= MidiSystem.getSequence(urlMusic2);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(64000);
}
}catch(Exception e){
}
}else{
try{
if(num==0){
sequence= MidiSystem.getSequence(urlMusic0);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(76000);
}else
if(num==1){
sequence= MidiSystem.getSequence(urlMusic1);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(140000);
}else{
sequence= MidiSystem.getSequence(urlMusic2);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(64000);
}
}catch(Exception e){
}
}
}
}
}
}
public static void main(String args[]) {
new Music();
}
}