Java简单MP3播放器
2015-10-02 11:06
351 查看
MusicPlayer类
package com.play;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.swing.JFrame;
public class MusicPlayer extends JFrame {
String filepath; //播放文件目录
String filename; //播放文件名称
AudioInputStream audioInputStream; //文件流
AudioFormat audioFormat; //文件格式
SourceDataLine sourceDataLine; //输出设备
boolean isStop = true; //控制播放线程
boolean hasStop = true; //播放线程状态
List list; //文件列表
Label labelfilepath; //播放目录显示标签
Label labelfilename; //播放文件显示标签
public MusicPlayer() {
//设置窗体属性
setLayout(new BorderLayout());
setTitle("MP3音乐播放器");
setSize(350, 370);
//建立菜单栏
MenuBar menubar = new MenuBar();
Menu menufile = new Menu("文件");
MenuItem menuopen = new MenuItem("打开", new MenuShortcut(KeyEvent.VK_O)) ; //new MenuItem("打 开 ", new MenuShortcut(KeyEvent.VK_O));
menufile.add(menuopen);
menufile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open();
}
});
menubar.add(menufile);
setMenuBar(menubar);
//文件列表
list = new List(10); //new List(10);
list.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
//双击时处理
if (e.getClickCount() == 2) {
//播放选中的文件
filename = list.getSelectedItem(); //"F:\\DJ Music\\(DJ)不想起床[1].mp3";//list.getSelectedItem();
play();
}
}
});
add(list, "Center");
//信息显示
Panel panel = new Panel(new GridLayout(2, 1));
labelfilepath = new Label("播放目录:");
labelfilename = new Label("播放文件:");
panel.add(labelfilepath);
panel.add(labelfilename);
add(panel, "North");
//注册窗体关闭事件
addWindowStateListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true); }
//打开
private void open() {
FileDialog dialog = new FileDialog(this, "Open", 0);
dialog.setVisible(true);
filepath = dialog.getDirectory();
if (filepath != null) {
labelfilepath.setText("播放目录:" + filepath);
//显示文件列表
list.removeAll();
File filedir = new File(filepath);
File[] filelist = filedir.listFiles();
for (File file : filelist) {
String filename = file.getName().toLowerCase();
if (filename.endsWith(".mp3") || filename.endsWith(".wav")) {
list.add(filename);
}
}
}
}
//播放
private void play() {
try {
isStop = true;
//停止播放线程
//等待播放线程停止
System.out.print("开始播放:" + filename.toString());
while (!hasStop) {
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
File file = new File(filepath + filename);
labelfilename.setText("播放文件:" + filename);
//取得文件输入流
audioInputStream = AudioSystem.getAudioInputStream(file);
if(audioInputStream!=null){
audioFormat = audioInputStream.getFormat();
}
//转换MP3文件编码
if(audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
audioFormat.getSampleRate(),
16,
audioFormat.getChannels(),
audioFormat.getChannels()*2,
audioFormat.getSampleRate(),
false);
audioInputStream = AudioSystem.getAudioInputStream(audioFormat, audioInputStream);
}
//打开输出设备
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED);
sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);
if(null== sourceDataLine){
sourceDataLine.open(audioFormat);
sourceDataLine.start();
}else{
sourceDataLine.stop();
sourceDataLine.drain();
sourceDataLine.open(audioFormat);
sourceDataLine.start();
}
//创建独立线程进行播放
isStop = false;
PlayThread playThread=new PlayThread(audioInputStream,sourceDataLine);
Thread thread = new Thread(playThread);
playThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
new MusicPlayer();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
PlayThread类
package com.play;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.SourceDataLine;
public class PlayThread extends Thread {
byte tempBuffer[] = new byte[320];
boolean isStop;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
public PlayThread(AudioInputStream audioInputStream,SourceDataLine sourceDataLine){
this.audioInputStream=audioInputStream;
this.sourceDataLine=sourceDataLine;
}
public void run() {
try {
int cnt=0;
boolean hasStop = false;
while ((cnt = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) {
if (isStop) break;
if (cnt > 0) {//写入缓存数据
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
//Block等待临时数据被输出为空 sourceDataLine.drain(); sourceDataLine.close(); hasStop = true;
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
package com.play;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.swing.JFrame;
public class MusicPlayer extends JFrame {
String filepath; //播放文件目录
String filename; //播放文件名称
AudioInputStream audioInputStream; //文件流
AudioFormat audioFormat; //文件格式
SourceDataLine sourceDataLine; //输出设备
boolean isStop = true; //控制播放线程
boolean hasStop = true; //播放线程状态
List list; //文件列表
Label labelfilepath; //播放目录显示标签
Label labelfilename; //播放文件显示标签
public MusicPlayer() {
//设置窗体属性
setLayout(new BorderLayout());
setTitle("MP3音乐播放器");
setSize(350, 370);
//建立菜单栏
MenuBar menubar = new MenuBar();
Menu menufile = new Menu("文件");
MenuItem menuopen = new MenuItem("打开", new MenuShortcut(KeyEvent.VK_O)) ; //new MenuItem("打 开 ", new MenuShortcut(KeyEvent.VK_O));
menufile.add(menuopen);
menufile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open();
}
});
menubar.add(menufile);
setMenuBar(menubar);
//文件列表
list = new List(10); //new List(10);
list.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
//双击时处理
if (e.getClickCount() == 2) {
//播放选中的文件
filename = list.getSelectedItem(); //"F:\\DJ Music\\(DJ)不想起床[1].mp3";//list.getSelectedItem();
play();
}
}
});
add(list, "Center");
//信息显示
Panel panel = new Panel(new GridLayout(2, 1));
labelfilepath = new Label("播放目录:");
labelfilename = new Label("播放文件:");
panel.add(labelfilepath);
panel.add(labelfilename);
add(panel, "North");
//注册窗体关闭事件
addWindowStateListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true); }
//打开
private void open() {
FileDialog dialog = new FileDialog(this, "Open", 0);
dialog.setVisible(true);
filepath = dialog.getDirectory();
if (filepath != null) {
labelfilepath.setText("播放目录:" + filepath);
//显示文件列表
list.removeAll();
File filedir = new File(filepath);
File[] filelist = filedir.listFiles();
for (File file : filelist) {
String filename = file.getName().toLowerCase();
if (filename.endsWith(".mp3") || filename.endsWith(".wav")) {
list.add(filename);
}
}
}
}
//播放
private void play() {
try {
isStop = true;
//停止播放线程
//等待播放线程停止
System.out.print("开始播放:" + filename.toString());
while (!hasStop) {
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
File file = new File(filepath + filename);
labelfilename.setText("播放文件:" + filename);
//取得文件输入流
audioInputStream = AudioSystem.getAudioInputStream(file);
if(audioInputStream!=null){
audioFormat = audioInputStream.getFormat();
}
//转换MP3文件编码
if(audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
audioFormat.getSampleRate(),
16,
audioFormat.getChannels(),
audioFormat.getChannels()*2,
audioFormat.getSampleRate(),
false);
audioInputStream = AudioSystem.getAudioInputStream(audioFormat, audioInputStream);
}
//打开输出设备
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED);
sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);
if(null== sourceDataLine){
sourceDataLine.open(audioFormat);
sourceDataLine.start();
}else{
sourceDataLine.stop();
sourceDataLine.drain();
sourceDataLine.open(audioFormat);
sourceDataLine.start();
}
//创建独立线程进行播放
isStop = false;
PlayThread playThread=new PlayThread(audioInputStream,sourceDataLine);
Thread thread = new Thread(playThread);
playThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
new MusicPlayer();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
PlayThread类
package com.play;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.SourceDataLine;
public class PlayThread extends Thread {
byte tempBuffer[] = new byte[320];
boolean isStop;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
public PlayThread(AudioInputStream audioInputStream,SourceDataLine sourceDataLine){
this.audioInputStream=audioInputStream;
this.sourceDataLine=sourceDataLine;
}
public void run() {
try {
int cnt=0;
boolean hasStop = false;
while ((cnt = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) {
if (isStop) break;
if (cnt > 0) {//写入缓存数据
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
//Block等待临时数据被输出为空 sourceDataLine.drain(); sourceDataLine.close(); hasStop = true;
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
相关文章推荐
- Java基础知识强化之集合框架笔记18:List集合特有的ListIterator迭代器
- Struts2 级联数据传递
- HDU 1715 大菲波数 (java)
- Struts2 获取表单请求参数
- Java虚拟机类加载机制
- java.lang.Exception: 索引中丢失 IN 或 OUT 参数::2 解决方法
- Pre-bound JDBC Connection found!解决办法
- eclipse黑底背景的设置
- java设计模式--单例模式
- HDOJ 1753 大明A+B (高精度的小数加法 java )
- JAVA阶段以及收银系统项目总结
- JAVA阶段以及收银系统项目总结
- 开启Java之路——不走弯路,就是捷径,欲速则不达
- spring源码
- java中的继承和接口
- Java中Runnable和Thread的区别
- Java基础知识强化之集合框架笔记17:List集合的特有的遍历功能
- Java基础知识强化之集合框架笔记16:List集合的特有功能概述和测试
- 使用JdbcTemplate进行插入操作
- java基础知识