图
17-7
音频输入输入流程图
为了支持
mp3
的播放,咱们必须添加
mp3
的
SPI
支持库。首先须要下载
mp3
的支持库:
●
官方网站:
http://www.javazoom.net/
●
下载页面:
http://www.javazoom.net/mp3spi/sources.html
●
下载地址:
http://www.javazoom.net/mp3spi/sources/mp3spi
1.9.4
.zip
●
下载文件:
mp3spi
1.9.4
.zip
将
mp3spi
1.9.4
.zip
中的三个
Jar
文件
jl1.0.jar
、
mp3spi1.9.4.jar
、
tritonus_share.jar
添加到当前项目中便可。
下面咱们来实现一个
MP3
音乐播放器,实现的功能是:单击【文件】
à
【打开】弹出文件选择对话框,将选择目录的全部
mp3
和
wav
文件加入到播放列表中,双击播放列表中的音乐文件实现音乐的播放和切换。
要实现该程序,首先须要提供了一个主程序窗口,该窗口提供了各类界面组件和操做事件的控制,并包含一个音乐播放的子线程,子线程由主线程控制。所以有两个类:
●
音乐播放器主程序
MusicPlayer.java
●
播放线程
PlayThread.java
(1)音乐播放器主程序MusicPlayer.java
package com.test.audio;
import java.io.File;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Frame;
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 javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class MusicPlayer extends Frame {
boolean isStop = true;//
控制播放线程
boolean hasStop = true;//
播放线程状态
String filepath;//
播放文件目录
String filename;//
播放文件名称
AudioInputStream audioInputStream;//
文件流
AudioFormat audioFormat;//
文件格式
SourceDataLine sourceDataLine;//
输出设备
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));
menufile.add(menuopen);
menufile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open();
}
});
menubar.add(menufile);
setMenuBar(menubar);
//
文件列表
list = new List(10);
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
//
双击时处理
if (e.getClickCount() == 2) {
//
播放选中的文件
filename = 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");
//
注册窗体关闭事件
addWindowListener(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);
while (!hasStop) {
System.out.print(".");
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
System.out.println("");
File file = new File(filepath + filename);
labelfilename.setText("
播放文件:
" + filename);
//
取得文件输入流
audioInputStream = AudioSystem.getAudioInputStream(file);
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);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
//
建立独立线程进行播放
isStop = false;
Thread playThread = new Thread(new PlayThread());
playThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
new MusicPlayer();
}
}
在该主程序中添加一个入口主函数
main()
来启动该主程序便可。
(2)播放线程PlayThread.java
该程序的代码以下:
class PlayThread extends Thread {
byte tempBuffer[] = new byte[320];
public void run() {
try {
int cnt;
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);
}
}
}
(3)运行MP3音乐播放器
完成以上代码后,便可运行主程序
MusicPlayer
,就会显示如图
17-9
示的窗口: