网上找了个,但是始终播放不了,哪位高手帮我看一下呀,谢谢,音频文件见附件 import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
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; public class PCMFilePlayerLeveler implements Runnable {
File file;
AudioInputStream in;
SourceDataLine line;
int frameSize;
byte[] buffer;
Thread playThread;
boolean playing;
boolean notYetEOF;
AudioFormat format;
float level; final static float MAX_8_BITS_SIGNED = Byte.MAX_VALUE;
final static float MAX_8_BITS_UNSIGNED = 0xff;
final static float MAX_16_BITS_SIGNED = Short.MAX_VALUE;
final static float MAX_16_BITS_UNSIGNED = 0xffff; public PCMFilePlayerLeveler(File f) throws IOException,
UnsupportedAudioFileException, LineUnavailableException {
file = f;
in = AudioSystem.getAudioInputStream (f);
// in = AudioSystem.getAudioInputStream (new BufferedInputStream (new FileInputStream(f)));
format = in.getFormat();
AudioFormat.Encoding formatEncoding = format.getEncoding();
if (!(formatEncoding.equals(AudioFormat.Encoding.PCM_SIGNED) || formatEncoding
.equals(AudioFormat.Encoding.PCM_UNSIGNED)))
throw new UnsupportedAudioFileException(file.getName()
+ " is not PCM audio");
System.out
.println("got PCM format: " + format.getChannels()
+ " channels, " + format.getSampleSizeInBits()
+ " bit samples");
frameSize = format.getFrameSize();
System.out.println("got frame size: ");
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
System.out.println("got info");
line = (SourceDataLine) AudioSystem.getLine(info); // figure out a small buffer size
int bytesPerSec = format.getSampleSizeInBits()
* (int) format.getSampleRate();
System.out.println("bytesPerSec = " + bytesPerSec);
int bufferSize = bytesPerSec / 20;
buffer = new byte[bufferSize]; System.out.println("got line");
line.open();
System.out.println("opened line");
playThread = new Thread(this);
playing = false;
notYetEOF = true;
playThread.start();
} public void run() {
int readPoint = 0;
int bytesRead = 0;
try {
while (notYetEOF) {
if (playing) {
// only write if the line will take at
// least a buffer-ful of data
if (line.available() < buffer.length) {
Thread.yield();
continue;
}
bytesRead = in.read(buffer, readPoint, buffer.length
- readPoint);
if (bytesRead == -1) {
notYetEOF = false;
break;
}
// how many frames did we get,
// and how many are left over?
int frames = bytesRead / frameSize;
int leftover = bytesRead % frameSize;
// calculate level
calculateLevel(buffer, readPoint, leftover);
// if (level > 1)
// System.out.println ("WTF? level = " + level);
// System.out.println ("level: " + level);
// send to line
line.write(buffer, readPoint, bytesRead - leftover);
// save the leftover bytes
System.arraycopy(buffer, bytesRead, buffer, 0, leftover);
readPoint = leftover; } else {
// if not playing
// Thread.yield();
try {
Thread.sleep(10);
} catch (InterruptedException ie) {
}
}
} // while notYetEOF
System.out.println("reached eof");
line.drain();
line.stop();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
// line.close();
}
} // run /**
* resets level by finding max value in buffer, taking into account whether
* these are 8 or 16 bit values (doesn't care about mono vs stereo - if one
* channel is disproportionately louder than the other, it wins)
*/
private void calculateLevel(byte[] buffer, int readPoint, int leftOver) {
int max = 0;
boolean use16Bit = (format.getSampleSizeInBits() == 16);
boolean signed = (format.getEncoding() == AudioFormat.Encoding.PCM_SIGNED);
boolean bigEndian = (format.isBigEndian());
if (use16Bit) {
for (int i = readPoint; i < buffer.length - leftOver; i += 2) {
int value = 0;
// deal with endianness
int hiByte = (bigEndian ? buffer[i] : buffer[i + 1]);
int loByte = (bigEndian ? buffer[i + 1] : buffer[i]);
if (signed) {
short shortVal = (short) hiByte;
shortVal = (short) ((shortVal << | (byte) loByte);
value = shortVal;
} else {
value = (hiByte << | loByte;
}
max = Math.max(max, value);
} // for
} else {
// 8 bit - no endianness issues, just sign
for (int i = readPoint; i < buffer.length - leftOver; i++) {
int value = 0;
if (signed) {
value = buffer[i];
} else {
short shortVal = 0;
shortVal = (short) (shortVal | buffer[i]);
value = shortVal;
}
max = Math.max(max, value);
} // for
} // 8 bit
// express max as float of 0.0 to 1.0 of max value
// of 8 or 16 bits (signed or unsigned)
if (signed) {
if (use16Bit) {
level = (float) max / MAX_16_BITS_SIGNED;
} else {
level = (float) max / MAX_8_BITS_SIGNED;
}
} else {
if (use16Bit) {
level = (float) max / MAX_16_BITS_UNSIGNED;
} else {
level = (float) max / MAX_8_BITS_UNSIGNED;
}
}
} // calculateLevel public void start() {
playing = true;
if (!playThread.isAlive())
playThread.start();
line.start();
} public void stop() {
playing = false;
line.stop();
} public SourceDataLine getLine() {
return line;
} public File getFile() {
return file;
} public float getLevel() {
return level;
} public static void main(String[] args) { try {
PCMFilePlayerLeveler ppl=new PCMFilePlayerLeveler(new File("c:/PCM.pcm"));
ppl.start(); } catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} } } /*
* Swing Hacks Tips and Tools for Killer GUIs By Joshua Marinacci, Chris Adamson
* First Edition June 2005 Series: Hacks ISBN: 0-596-00907-0
* http://www.oreilly.com/catalog/swinghks/
*/
import java.io.File;
import java.io.FileInputStream;
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; public class PCMFilePlayerLeveler implements Runnable {
File file;
AudioInputStream in;
SourceDataLine line;
int frameSize;
byte[] buffer;
Thread playThread;
boolean playing;
boolean notYetEOF;
AudioFormat format;
float level; final static float MAX_8_BITS_SIGNED = Byte.MAX_VALUE;
final static float MAX_8_BITS_UNSIGNED = 0xff;
final static float MAX_16_BITS_SIGNED = Short.MAX_VALUE;
final static float MAX_16_BITS_UNSIGNED = 0xffff; public PCMFilePlayerLeveler(File f) throws IOException,
UnsupportedAudioFileException, LineUnavailableException {
file = f;
in = AudioSystem.getAudioInputStream (f);
// in = AudioSystem.getAudioInputStream (new BufferedInputStream (new FileInputStream(f)));
format = in.getFormat();
AudioFormat.Encoding formatEncoding = format.getEncoding();
if (!(formatEncoding.equals(AudioFormat.Encoding.PCM_SIGNED) || formatEncoding
.equals(AudioFormat.Encoding.PCM_UNSIGNED)))
throw new UnsupportedAudioFileException(file.getName()
+ " is not PCM audio");
System.out
.println("got PCM format: " + format.getChannels()
+ " channels, " + format.getSampleSizeInBits()
+ " bit samples");
frameSize = format.getFrameSize();
System.out.println("got frame size: ");
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
System.out.println("got info");
line = (SourceDataLine) AudioSystem.getLine(info); // figure out a small buffer size
int bytesPerSec = format.getSampleSizeInBits()
* (int) format.getSampleRate();
System.out.println("bytesPerSec = " + bytesPerSec);
int bufferSize = bytesPerSec / 20;
buffer = new byte[bufferSize]; System.out.println("got line");
line.open();
System.out.println("opened line");
playThread = new Thread(this);
playing = false;
notYetEOF = true;
playThread.start();
} public void run() {
int readPoint = 0;
int bytesRead = 0;
try {
while (notYetEOF) {
if (playing) {
// only write if the line will take at
// least a buffer-ful of data
if (line.available() < buffer.length) {
Thread.yield();
continue;
}
bytesRead = in.read(buffer, readPoint, buffer.length
- readPoint);
if (bytesRead == -1) {
notYetEOF = false;
break;
}
// how many frames did we get,
// and how many are left over?
int frames = bytesRead / frameSize;
int leftover = bytesRead % frameSize;
// calculate level
calculateLevel(buffer, readPoint, leftover);
// if (level > 1)
// System.out.println ("WTF? level = " + level);
// System.out.println ("level: " + level);
// send to line
line.write(buffer, readPoint, bytesRead - leftover);
// save the leftover bytes
System.arraycopy(buffer, bytesRead, buffer, 0, leftover);
readPoint = leftover; } else {
// if not playing
// Thread.yield();
try {
Thread.sleep(10);
} catch (InterruptedException ie) {
}
}
} // while notYetEOF
System.out.println("reached eof");
line.drain();
line.stop();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
// line.close();
}
} // run /**
* resets level by finding max value in buffer, taking into account whether
* these are 8 or 16 bit values (doesn't care about mono vs stereo - if one
* channel is disproportionately louder than the other, it wins)
*/
private void calculateLevel(byte[] buffer, int readPoint, int leftOver) {
int max = 0;
boolean use16Bit = (format.getSampleSizeInBits() == 16);
boolean signed = (format.getEncoding() == AudioFormat.Encoding.PCM_SIGNED);
boolean bigEndian = (format.isBigEndian());
if (use16Bit) {
for (int i = readPoint; i < buffer.length - leftOver; i += 2) {
int value = 0;
// deal with endianness
int hiByte = (bigEndian ? buffer[i] : buffer[i + 1]);
int loByte = (bigEndian ? buffer[i + 1] : buffer[i]);
if (signed) {
short shortVal = (short) hiByte;
shortVal = (short) ((shortVal << | (byte) loByte);
value = shortVal;
} else {
value = (hiByte << | loByte;
}
max = Math.max(max, value);
} // for
} else {
// 8 bit - no endianness issues, just sign
for (int i = readPoint; i < buffer.length - leftOver; i++) {
int value = 0;
if (signed) {
value = buffer[i];
} else {
short shortVal = 0;
shortVal = (short) (shortVal | buffer[i]);
value = shortVal;
}
max = Math.max(max, value);
} // for
} // 8 bit
// express max as float of 0.0 to 1.0 of max value
// of 8 or 16 bits (signed or unsigned)
if (signed) {
if (use16Bit) {
level = (float) max / MAX_16_BITS_SIGNED;
} else {
level = (float) max / MAX_8_BITS_SIGNED;
}
} else {
if (use16Bit) {
level = (float) max / MAX_16_BITS_UNSIGNED;
} else {
level = (float) max / MAX_8_BITS_UNSIGNED;
}
}
} // calculateLevel public void start() {
playing = true;
if (!playThread.isAlive())
playThread.start();
line.start();
} public void stop() {
playing = false;
line.stop();
} public SourceDataLine getLine() {
return line;
} public File getFile() {
return file;
} public float getLevel() {
return level;
} public static void main(String[] args) { try {
PCMFilePlayerLeveler ppl=new PCMFilePlayerLeveler(new File("c:/PCM.pcm"));
ppl.start(); } catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} } } /*
* Swing Hacks Tips and Tools for Killer GUIs By Joshua Marinacci, Chris Adamson
* First Edition June 2005 Series: Hacks ISBN: 0-596-00907-0
* http://www.oreilly.com/catalog/swinghks/
*/
http://www.blogjava.net/shinzey/articles/163573.html
import javax.swing.*;public class JukeBox
{
public static void main (String[] args) throws Exception
{
JFrame frame=new JFrame("JukeBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().add(new JukeBoxControls());frame.pack();
frame.setVisible(true);
}
}
//控制面板import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.AudioClip;
import java.net.URL;public class JukeBoxControls extends JPanel
{
private JComboBox musicCombo;
private JButton stopButton,playButton;
private AudioClip[] music;
private AudioClip current;public JukeBoxControls()
{
URL url1,url2,url3,url4,url5,url6,url;
url1=url2=url3=url4=url5=url6=null;try
{
url1=new URL("file","localhost","The climb.wav");
url2=new URL("file","localhost","Burning-Maria Arredondo.wav");
url3=new URL("file","localhost","You Are Not Alone-Michael.wav");
url4=new URL("file","localhost","Love Story-Taylor Swift.wav");
url5=new URL("file","localhost","星月神话.wav");
url6=new URL("file","localhost","西城男孩 - soledad.wav");}catch(Exception e){}//Creats the list.
music=new AudioClip[7];
music[0]=null;
music[1]=JApplet.newAudioClip(url1);
music[2]=JApplet.newAudioClip(url2);
music[3]=JApplet.newAudioClip(url3);
music[4]=JApplet.newAudioClip(url4);
music[5]=JApplet.newAudioClip(url5);
music[6]=JApplet.newAudioClip(url6);String[] musicNames={"Make a select...","The climb","Burning-Maria Arredondo",
"You Are Not Alone-Michael","Love Story-Taylor Swift","星月神话","西城男孩 - soledad"};musicCombo=new JComboBox(musicNames);
musicCombo.setBackground(Color.ORANGE);//Set up the buttons
playButton=new JButton("Play",new ImageIcon("play.jpg"));
playButton.setBackground(Color.ORANGE);
stopButton=new JButton("stop",new ImageIcon("stop.jpg"));
stopButton.setBackground(Color.ORANGE);//set up the panel.
setPreferredSize(new Dimension(250,100));
setBackground(Color.ORANGE);
add(musicCombo);
add(playButton);
add(stopButton);musicCombo.addActionListener(new ComboListener());
playButton.addActionListener(new ButtonListener());
stopButton.addActionListener(new ButtonListener());
current=null;}private class ComboListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(current!=null)
current.stop();
current=music[musicCombo.getSelectedIndex()];
}
}private class ButtonListener implements ActionListener
{public void actionPerformed(ActionEvent e)
{
if(current!=null)
current.stop();
if(e.getSource()==playButton)
if(current!=null)
current.play();
}
}
}