java 播放mp3_使用Java Sound API播放MP3

本文介绍了如何使用Java Sound API在Java应用中播放MP3文件。由于Java Sound默认不支持MP3,因此需要添加如mp3plugin.jar的依赖来支持。文章提供了一个名为BigClip的类,该类扩展了javax.sound.sampled.Clip接口,能够处理大容量的音频数据,并实现了播放、循环、定位等操作。此外,还详细解释了类的内部工作原理,包括转换音频格式、处理循环点和调整播放速度等功能。
摘要由CSDN通过智能技术生成

如前所述,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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值