來源:寧靜志遠 淡泊明志
昨天在QQ群上有人問題使用APPLET播放WAV的事情,自己還真沒有使用java 寫過多媒體處理,在網上找了一下相關的文章,有提到JMF框架的,於是找來安裝一下,沒有深入的研究JMF,初步覺得,使用方便,但是同時也給運行環境增加額外的負擔。
然後看到《用Java實現音頻播放》這篇文章,文章沒有作者信息,但是可以在天極找到原文。如有需要可以根據下邊的鏈接找到文章。
文章對javax.sound包的結構作了深入的講解,而且為MP3解碼提供的解決方案。最後介紹了一個簡單播放器的開發,因為篇幅,作者沒有把完 整的代碼送上,我仿照作者的思路,寫了個簡單的播放WAV文件的代碼,撇開複雜的線程,目的在展示播放WAV文件的實現代碼上。綿薄的功夫而已。
源代碼
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;
/**
* @author Administrator
*
*/
public class WavPlayer extends JFrame {
/**
*
*/
private AudioInputStream ais;
private String fileUrl="WindowsDing.wav";
//文件過濾器
private class MyFilter extends FileFilter{
@Override
public boolean accept(File arg0) {
// TODO Auto-generated method stub
if(arg0.getName().endsWith(".wav" )|| arg0.isDirectory())
return true;
return false;
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return "波形文件(.wav)";
}
}
WavPlayer(){
super();
setSize(150,150);
JButton startBut=new JButton("播放");
startBut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JFileChooser jfc= new JFileChooser();
jfc.setFileFilter(new MyFilter());
if(jfc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
fileUrl=jfc.getSelectedFile().getAbsolutePath();
else
return;
try {
ais=AudioSystem.getAudioInputStream(new File(fileUrl));
AudioFormat af=ais.getFormat();
SourceDataLine sdl=null;
DataLine.Info info=new DataLine.Info(SourceDataLine.class,af);
sdl=(SourceDataLine) AudioSystem.getLine(info);
sdl.open(af);
sdl.start();
//play
int nByte=0;
byte[] buffer=new byte[128];
while(nByte!=-1){
nByte=ais.read(buffer,0,128);
if(nByte>=0){
int oByte=sdl.write(buffer, 0, nByte);
System.out.println(oByte);
}
}
sdl.stop();
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
this.getContentPane().add(startBut);
setVisible(true);
}
public static void main(String[] args){
new WavPlayer();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;
/**
* @author Administrator
*
*/
public class WavPlayer extends JFrame {
/**
*
*/
private AudioInputStream ais;
private String fileUrl="WindowsDing.wav";
//文件過濾器
private class MyFilter extends FileFilter{
@Override
public boolean accept(File arg0) {
// TODO Auto-generated method stub
if(arg0.getName().endsWith(".wav" )|| arg0.isDirectory())
return true;
return false;
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return "波形文件(.wav)";
}
}
WavPlayer(){
super();
setSize(150,150);
JButton startBut=new JButton("播放");
startBut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JFileChooser jfc= new JFileChooser();
jfc.setFileFilter(new MyFilter());
if(jfc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
fileUrl=jfc.getSelectedFile().getAbsolutePath();
else
return;
try {
ais=AudioSystem.getAudioInputStream(new File(fileUrl));
AudioFormat af=ais.getFormat();
SourceDataLine sdl=null;
DataLine.Info info=new DataLine.Info(SourceDataLine.class,af);
sdl=(SourceDataLine) AudioSystem.getLine(info);
sdl.open(af);
sdl.start();
//play
int nByte=0;
byte[] buffer=new byte[128];
while(nByte!=-1){
nByte=ais.read(buffer,0,128);
if(nByte>=0){
int oByte=sdl.write(buffer, 0, nByte);
System.out.println(oByte);
}
}
sdl.stop();
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
this.getContentPane().add(startBut);
setVisible(true);
}
public static void main(String[] args){
new WavPlayer();
}
}
需要指出的
需要指出的一點是,
DataLine.Info info=new DataLine.Info(SourceDataLine.class,af);
sdl=(SourceDataLine) AudioSystem.getLine(info);
sdl.open(af);
sdl.start();
sourceDataLine在被寫入前需要被打開" sdl.start();" 這點作者沒有說明。
0 留言:
張貼留言