http://www.yooongchun.cn/
摘要:本文使用java基础技术实现了一个可播放mid、wav格式音乐的简易音乐播放器,带UI
package com.music;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import java.awt.*;
public class playAudio extends JFrame implements ActionListener {
static String filePath =null;
JButton btPause, btExit, btPlay, btLoop, btStop;
JPanel panel;
JMenuBar mb;
JMenu menu;
JMenuItem menuMi1,menuMi2;
JTextArea textarea;
public static void main(String[] args) {
playAudio pa = new playAudio();
}
public playAudio() {
btPause = new JButton("Pause");
btPause.setFont(new Font("Chaparral Pro Light",Font.BOLD,15));
btExit = new JButton("EXIT");
btExit.setFont(new Font("Chaparral Pro Light",Font.BOLD,15));
btPlay = new JButton("PLAY");
btPlay.setFont(new Font("Chaparral Pro Light",Font.BOLD,15));
btLoop = new JButton("LOOP");
btLoop.setFont(new Font("Chaparral Pro Light",Font.BOLD,15));
btStop = new JButton("STOP");
btStop.setFont(new Font("Chaparral Pro Light",Font.BOLD,15));
textarea=new JTextArea("Import music fileDialog...");
textarea.setEditable(false);
textarea.setFont(new Font("Chaparral Pro Light",Font.HANGING_BASELINE,20));
mb=new JMenuBar();
menu=new JMenu("File");
menu.setMnemonic('F');
menu.setFont(new Font("Chaparral Pro Light",Font.HANGING_BASELINE,15));
menuMi1=new JMenuItem("Open");
menuMi1.setMnemonic('O');
menuMi1.setFont(new Font("Chaparral Pro Light",Font.HANGING_BASELINE,15));
menuMi2=new JMenuItem("Exit");
menuMi2.setMnemonic('E');
menuMi2.setFont(new Font("Chaparral Pro Light",Font.HANGING_BASELINE,15));
panel = new JPanel();
panel.add(btPlay);
panel.add(btLoop);
panel.add(btPause);
panel.add(btStop);
panel.add(btExit);
mb.add(menu);
menu.add(menuMi1);
menu.add(menuMi2);
btPause.addActionListener(this);
btPause.setActionCommand("pause");
btExit.addActionListener(this);
btExit.setActionCommand("exit");
btPlay.addActionListener(this);
btPlay.setActionCommand("play");
btLoop.addActionListener(this);
btLoop.setActionCommand("loop");
btStop.addActionListener(this);
btStop.setActionCommand("stop");
menuMi1.addActionListener(this);
menuMi1.setActionCommand("open");
menuMi2.addActionListener(this);
menuMi2.setActionCommand("exit");
this.setJMenuBar(mb);
this.add(textarea);
this.add(panel,BorderLayout.SOUTH);
this.setIconImage(new ImageIcon("image/music.JPG").getImage());
this.setTitle("MusicPlayer");
this.setSize(400, 150);
this.setLocation(300, 260);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String text="";
Audio player = new Audio(filePath);
if (e.getActionCommand().equals("open")) {
FileDialog fd = new FileDialog(this,"Chooes music", FileDialog.LOAD);
fd.setVisible(true);
filePath = fd.getDirectory() +fd.getFile();
if(filePath!=null)
System.out.println(text="filePath is: "+filePath);
else
System.out.println(text="Couldn't open file");
}
else if (e.getActionCommand().equals("exit")) {
player.interrupt();
System.out.println(text="Exit program!");
System.exit(0);
}
else if (e.getActionCommand().equals("play")) {
if(filePath!=null){
player.start();
System.out.println(text="Play music: "+filePath);
}else
System.out.println(text="There is not any file!");
}
else if (e.getActionCommand().equals("loop")) {
if(filePath!=null){
while(!player.isAlive()){
try{
player.start();
System.out.println(text="Loop playing"+filePath);
}catch(Exception e1){
System.err.println(text="Loop Error");
}
}
}else
System.out.println(text="There is not any file!");
}
else if (e.getActionCommand().equals("stop")) {
player.interrupt();
System.out.println(text="Stop playing!");
}
else if(e.getActionCommand().equals("pause")){
player.interrupt();
System.out.println(text="Pause playing!");
}
this.textarea.setText(text);
}
}
class Audio extends Thread {
private String filename;
public Audio(String filename) {
this.filename = filename;
}
public void run() {
File sourceFile = new File(filename);
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(sourceFile);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[1024];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
auline.drain();
auline.close();
}
}
}