java写mp3播放器
用java写个播放程序,只有加载文件(wav或者mp3都行)和播放,不需要有界面,不需要其他控制,不添加窗口类组件,代码越简单越好,只要有声音出来就行...
用java写个播放程序,只有加载文件(wav或者mp3都行)和播放,不需要有界面,不需要其他控制,不添加窗口类组件,代码越简单越好,只要有声音出来就行
展开
展开全部
--------------不支持MP3---------------------
public AudioClip loadSound(String filename) { // 返回一个AudioClip对象
URL url = null; // 因为newAudioClip()的参数为URL型
try {
url = new URL("file:" + filename); // 指定文件,“file:"不能少
} catch (MalformedURLException e) {
}
return JApplet.newAudioClip(url); // 通过newAudioClip(
// )方法装载声音,此方法为JDK后添加的方法,太老的JDK里可能没有
}
AudioClip s1 = loadSound("声音//TableStopGetPrice.wav");
s1.play();
------------------支持mp3--------------------------
见附件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.BitstreamException;
import javazoom.jl.decoder.Decoder;
import javazoom.jl.decoder.Header;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.decoder.SampleBuffer;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
/**
* The <code>Player</code> class implements a simple player for playback of an
* MPEG audio stream.
*
* @author Mat McGowan
* @since 0.0.8
*/
public class Player
{
/**
* The current frame number.
*/
private int frame = 0;
/**
* The MPEG audio bitstream.
*/
// javac blank final bug.
/* final */private Bitstream bitstream;
/**
* The MPEG audio decoder.
*/
/* final */private Decoder decoder;
/**
* The AudioDevice the audio samples are written to.
*/
private AudioDevice audio;
/**
* Has the player been closed?
*/
private boolean closed = false;
/**
* Has the player played back all frames from the stream?
*/
private boolean complete = false;
private int lastPosition = 0;
/**
* Creates a new <code>Player</code> instance.
*/
public Player ( InputStream stream ) throws JavaLayerException
{
this (stream, null);
}
public Player ( InputStream stream, AudioDevice device ) throws JavaLayerException
{
bitstream = new Bitstream (stream);
decoder = new Decoder ();
if (device != null)
{
audio = device;
}
else
{
FactoryRegistry r = FactoryRegistry.systemRegistry ();
audio = r.createAudioDevice ();
}
audio.open (decoder);
}
public void play () throws JavaLayerException
{
play (Integer.MAX_VALUE);
}
/**
* Plays a number of MPEG audio frames.
*
* @param frames
* The number of frames to play.
* @return true if the last frame was played, or false if there are more
* frames.
*/
public boolean play ( int frames ) throws JavaLayerException
{
boolean ret = true;
while (frames-- > 0 && ret)
{
ret = decodeFrame ();
}
if (!ret)
{
// last frame, ensure all data flushed to the audio device.
AudioDevice out = audio;
if (out != null)
{
out.flush ();
synchronized (this)
{
complete = ( !closed );
close ();
}
}
}
return ret;
}
/**
* Cloases this player. Any audio currently playing is stopped immediately.
*/
public synchronized void close ()
{
AudioDevice out = audio;
if (out != null)
{
closed = true;
audio = null;
// this may fail, so ensure object state is set up before
// calling this method.
out.close ();
lastPosition = out.getPosition ();
try
{
bitstream.close ();
}
catch (BitstreamException ex)
{}
}
}
/**
* Returns the completed status of this player.
*
* @return true if all available MPEG audio frames have been decoded, or
* false otherwise.
*/
public synchronized boolean isComplete ()
{
return complete;
}
/**
* Retrieves the position in milliseconds of the current audio sample being
* played. This method delegates to the <code>
* AudioDevice</code> that is used by this player to sound the decoded audio
* samples.
*/
public int getPosition ()
{
int position = lastPosition;
AudioDevice out = audio;
if (out != null)
{
position = out.getPosition ();
}
return position;
}
/**
* Decodes a single frame.
*
* @return true if there are no more frames to decode, false otherwise.
*/
protected boolean decodeFrame () throws JavaLayerException
{
try
{
AudioDevice out = audio;
if (out == null)
return false;
Header h = bitstream.readFrame ();
if (h == null)
return false;
// sample buffer set when decoder constructed
SampleBuffer output = (SampleBuffer) decoder.decodeFrame (h, bitstream);
synchronized (this)
{
out = audio;
if (out != null)
{
out.write (output.getBuffer (), 0, output.getBufferLength ());
}
}
bitstream.closeFrame ();
}
catch (RuntimeException ex)
{
throw new JavaLayerException ("Exception decoding audio frame", ex);
}
/*
* catch (IOException ex) {
* System.out.println("exception decoding audio frame: "+ex); return
* false; } catch (BitstreamException bitex) {
* System.out.println("exception decoding audio frame: "+bitex); return
* false; } catch (DecoderException decex) {
* System.out.println("exception decoding audio frame: "+decex); return
* false; }
*/
return true;
}
public static void main ( String[] args )
{
try
{
Player player = new Player (new FileInputStream (new File ("D:\\Youdagames\\JLayer1.0.1\\abc.mp3")));
player.play ();
}
catch (FileNotFoundException e)
{
e.printStackTrace ();
}
catch (JavaLayerException e)
{
e.printStackTrace ();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询