📄 mp3player.java
字号:
package org.serain.shmily.player;import java.io.*;import javazoom.jl.player.*;import javazoom.jl.decoder.*;public class Mp3Player extends Thread { private boolean play=true; private boolean stop=false; public Mp3Player(File file) throws FileNotFoundException, JavaLayerException{ this(new BufferedInputStream(new FileInputStream(file))); } public void pause(){ play=false; } public void play(){ play=true; } public void stope(){ stop=true; close(); } public void run() { boolean ret = true; int frames=Integer.MAX_VALUE; while(!stop){ if(play==true){ if ((frames-- > 0)==true && ret==true){ try { ret = decodeFrame(); } catch (JavaLayerException ex) { ex.printStackTrace(); } } if (!ret){ stop=true; AudioDevice out = audio; if (out!=null){ out.flush(); synchronized (this){ complete = (!closed); close(); } } synchronized(this){ notify();//唤醒歌曲切换监听器 } } } } } public boolean isPlay() { return play; } public void setPlay(boolean play) { this.play = play; } public boolean isStop() { return stop; } public void setStop(boolean stop) { this.stop = stop; } public boolean isClosed() { return closed; } public void setClosed(boolean closed) { this.closed = closed; } /** * 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 Mp3Player(InputStream stream) throws JavaLayerException { this(stream, null); } public Mp3Player(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); } /** * 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); } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -