如前所述,Java Sound默认不支持MP3。对于它在任何特定JRE中支持的类型,请检查AudioSystem.getAudioFileTypes()。
添加对MP3的支持的一种方法是将基于JMF的mp3plugin.jar1添加到应用程序的运行时类路径中。众所周知,这种联系并不完全可靠。Jar也可以在我的软件共享驱动器上使用。
至于你的实际问题,虽然javax.sound.sampled.Clip看起来似乎是这类任务的理想选择,但遗憾的是它只能保持立体声,16位,44.1KHz的声音。这就是我开发的原因BigClip。(它有自己的循环问题,如果你修复它们,报告回来。)package org.pscode.xui.sound.bigclip;import java.awt.Component;import javax.swing.*;// J2SE 1.3import javax.sound.sampled.*;import java.io.*;// J2SE 1.4import java.util.logging.*;import java.util.Arrays;/** An implementation of the javax.sound.sampled.Clip that is designed
to handle Clips of arbitrary size, limited only by the amount of memory
available to the app. It uses the post 1.4 thread behaviour (daemon thread)
that will stop the sound running after the main has exited.
- 2012-07-24 - Fixed bug in size of byte array (2^16 -> (int)Math.pow(2, 16)).
- 2009-09-01 - Fixed bug that had clip ..clipped at the end, by calling drain() (before
calling stop()) on the dataline after the play loop was complete. Improvement to frame
and microsecond position determination.
- 2009-08-17 - added convenience constructor that accepts a Clip. Changed the private
convertFrameToM..seconds methods from 'micro' to 'milli' to reflect that they were dealing
with units of 1000/th of a second.
- 2009-08-14 - got rid of flush() after the sound loop, as it was cutting off tracks just
before the end, and was found to be not needed for the fast-forward/rewind functionality it
was introduced to support.
- 2009-08-11 - First binary release.
N.B. Remove @Override notation and logging to use in 1.3+
@since 1.5
@version 2009-08-17
@author Andrew Thompson */public class BigClip implements Clip, LineListener {
/** The DataLine used by this Clip. */
private SourceDataLine dataLine;
/** The raw bytes of the audio data. */
private byte[] audioData;
/** The stream wrapper for the audioData. */
private ByteArrayInputStream inputStream;
/** Loop count set by the calling code. */
private int loopCount;
/** Internal count of how many loops to go. */
private int countDown;
/** The start of a loop point. Defaults to 0. */
private int loopPointStart;
/** The end of a loop point. Defaults to the end of the Clip. */
private int loopPointEnd;
/** Stores the current frame position of the clip. */
private int framePosition;
/** Thread used to run() sound. */
private Thread thread;
/** Whether the sound is currently playing or active. */
private boolean active;
/** Stores the last time bytes were dumped to the audio stream. */
private long timelastPositionSet;
private int bufferUpdateFactor = 2;
/** The parent Component for the loading progress dialog. */
Component parent = null;
/** Used for reporting messages. */
private Logger logger = Logger.getAnonymousLogger();
/** Default constructor for a BigClip. Does nothing. Information from the
AudioInputStream passed in open() will be used to get an appropriate SourceDataLine. */
public BigClip() {}
/** There are a number of AudioSystem methods that will return a configured Clip. This
convenience constructor allows us to obtain a SourceDataLine for the BigClip that uses
the same AudioFormat as the original Clip.
@param clip Clip The Clip used to configure the BigClip. */
public BigClip(Clip clip) throws LineUnavailableException {
dataLine = AudioSystem.getSourceDataLine( clip.getFormat() );
}
/** Provides the entire audio buffer of this clip.
@return audioData byte[] The bytes of the audio data that is loaded in this Clip. */
public byte[] getAudioData() {
return audioData;
}
/** Sets a parent component to act as owner of a "Loading track.." progress dialog.
If null, there will be no progress shown. */
public void setParentComponent(Component parent) {
this.parent = parent;
}
/** Converts a frame count to a duration in milliseconds. */
private long convertFramesToMilliseconds(int frames) {
return (frames/(long)dataLine.getFormat().getSampleRate())*1000;