⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 midiapplet.java

📁 Application & Integration Services are at the heart of any Service Oriented Architecture (SOA). Sun
💻 JAVA
字号:
import javax.swing.*;
import java.awt.event.*;
import javax.sound.midi.*;
import java.awt.GridLayout;
import java.io.File;
public class MidiApplet //extends javax.swing.JApplet
{
	private JFrame jf;
	JPanel pane;
	public void init()
	{
		 pane=new JPanel();
		MidiPlayer midi =new MidiPlayer("music\\4.mid");
		pane.add(midi);
		//setContentPane(pane);
	}
	public MidiApplet()
	{
	 	jf=new JFrame("music");
	 	init();
	    jf.setSize(250,120)		;
	    jf.getContentPane().add(pane);
	    jf.show();
	}
	public static void main(String[] arg)
	{
		MidiApplet midi=new MidiApplet();
		
	}
}
class MidiPlayer extends JPanel implements Runnable,ActionListener
{
	Thread runner;
	JButton play=new JButton("Play");
	JButton stop =new JButton("Stop");
	JLabel message=new JLabel();
	JComboBox tempoBox=new JComboBox();
	float tempo=1.0F;
	Sequence currentSound;
	Sequencer player;
	String songFile;
	MidiPlayer(String song)
	{
		super();
		songFile=song;
		play.addActionListener(this);
		for(float i=0.25F;i<7F;i+=0.25F)
		tempoBox.addItem(""+i);
		tempoBox.setSelectedItem("1.0");
		tempoBox.setEnabled(false);
		tempoBox.addActionListener(this);
		setLayout(new GridLayout(2,1));
		add(message);
		JPanel buttons=new JPanel();
		JLabel tempoLabel=new JLabel("tempo:");
		buttons.add(play);
		buttons.add(stop);
		buttons.add(tempoLabel);
		buttons.add(tempoBox);
		add(buttons);
		if(songFile==null)
		{
			play.setEnabled(false);
		}
	}
		public void actionPerformed(ActionEvent evt)
		{
			if(evt.getSource() instanceof JButton)
			{
				if(evt.getSource()==play)
				play();
				else
				stop1();
			}
				else
				{
					String item=(String )tempoBox.getSelectedItem();
					try
					{
						tempo=Float.parseFloat(item);
						player.setTempoFactor(tempo);
						message.setText("Playing  "+songFile+"  at  "+tempo+" tempo");
					}
						catch(NumberFormatException ex)
						{
							message.setText(ex.toString());
						}
						
				}
		}
	
	void play()
	{
		if(runner==null)
		{
			runner=new Thread(this);
			runner.start();
			//play.setEnabled(false);
			stop.setEnabled(true);
			tempoBox.setEnabled(true);
		}
	}
	void stop1()
	{
		if(runner!=null)
		{
			runner=null;
			//player.stop();
			System.out.println("stop");		
			play.setEnabled(true);
			stop.setEnabled(false);
			tempoBox.setEnabled(false);
		}
		else
			System.out.println("stop ok");	
		
	}
	public void run()
	{
		try
		{
			File song=new File(songFile);
			currentSound=MidiSystem.getSequence(song);
			player=MidiSystem.getSequencer();
						
		}
		catch (Exception ex)
		{
			message.setText(ex.toString());
		}
		Thread thisThread =Thread.currentThread();
		while (runner==thisThread)
		{
			try
			{		
			player.open();
			player.setSequence(currentSound);
			player.setTempoFactor(tempo);
			player.start();			
			message.setText("Playing "+songFile+" at "+tempo+" tempo");
			while(player.isRunning() && runner!=null)
			 {
				try
				{
					Thread.sleep(10000);
				//	System.out.println("time:"+(System.currentTimeMillis()/10000)+"running");
				//	System.out.println("start");
					
				}
				catch(InterruptedException e)
				{
				}
			 }
			
		   	message.setText("");
		   	System.out.println("time:"+(System.currentTimeMillis()/10000)+"running");
		   	//Thread.sleep(10000);
		   	//runner=null;
			player.close();
		   }
			catch(Exception ex)
			{
				message.setText(ex.toString());
				break;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -