⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicplayer.java

📁 YOYOPlayer MP3播放器 java+JMF实现
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * BasicPlayer. * * JavaZOOM : jlgui@javazoom.net *            http://www.javazoom.net * *----------------------------------------------------------------------- *   This program is free software; you can redistribute it and/or modify *   it under the terms of the GNU Library General Public License as published *   by the Free Software Foundation; either version 2 of the License, or *   (at your option) any later version. * *   This program is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *   GNU Library General Public License for more details. * *   You should have received a copy of the GNU Library General Public *   License along with this program; if not, write to the Free Software *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *---------------------------------------------------------------------- */package com.hadeslee.yoyoplayer.player;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;import javax.sound.sampled.AudioFileFormat;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.Control;import javax.sound.sampled.DataLine;import javax.sound.sampled.FloatControl;import javax.sound.sampled.Line;import javax.sound.sampled.LineUnavailableException;import javax.sound.sampled.Mixer;import javax.sound.sampled.SourceDataLine;import javax.sound.sampled.UnsupportedAudioFileException;import javazoom.spi.PropertiesContainer;import org.tritonus.share.sampled.TAudioFormat;import org.tritonus.share.sampled.file.TAudioFileFormat;import static com.hadeslee.yoyoplayer.player.BasicPlayerEvent.*;/** * BasicPlayer is a threaded simple player class based on JavaSound API. * It has been successfully tested under J2SE 1.3.x, 1.4.x and 1.5.x. */@SuppressWarnings("unchecked")public class BasicPlayer implements BasicController, Runnable {    public static int EXTERNAL_BUFFER_SIZE = 4000 * 4;    public static int SKIP_INACCURACY_SIZE = 512;    protected Thread m_thread = null;    protected Object m_dataSource;    protected AudioInputStream m_encodedaudioInputStream;    protected int encodedLength = -1;    protected AudioInputStream m_audioInputStream;    protected AudioFileFormat m_audioFileFormat;    protected SourceDataLine m_line;    protected FloatControl m_gainControl;    protected FloatControl m_panControl;    protected String m_mixerName = null;    private int m_lineCurrentBufferSize = -1;    private int lineBufferSize = -1;    private long threadSleep = -1;    private static Logger log = Logger.getLogger(BasicPlayer.class.getName());    /**     * These variables are used to distinguish stopped, paused, playing states.     * We need them to control Thread.     *///    public static final int UNKNOWN = -1;//    public static final int PLAYING = 0;//    public static final int PAUSED = 1;//    public static final int STOPPED = 2;//    public static final int OPENED = 3;//    public static final int SEEKING = 4;    private int m_status = UNKNOWN;    private Map empty_map = new HashMap();    private BasicPlayerEventLauncher laucher;//事件分派器    /**     * Constructs a Basic Player.     */    public BasicPlayer() {        m_dataSource = null;        laucher = new BasicPlayerEventLauncher();        laucher.start();        reset();    }    protected void reset() {        m_status = UNKNOWN;        if (m_audioInputStream != null) {            synchronized (m_audioInputStream) {                closeStream();            }        }        m_audioInputStream = null;        m_audioFileFormat = null;        m_encodedaudioInputStream = null;        encodedLength = -1;        if (m_line != null) {            m_line.stop();            m_line.close();            m_line = null;        }        m_gainControl = null;        m_panControl = null;    }    /**     * Add listener to be notified.     * @param bpl     */    public void addBasicPlayerListener(BasicPlayerListener bpl) {        laucher.addBasicPlayerListener(bpl);    }    /**     * Return registered listeners.     * @return     */    public Collection getListeners() {        return laucher.getBasicPlayerListeners();    }    /**     * Remove registered listener.     * @param bpl     */    public void removeBasicPlayerListener(BasicPlayerListener bpl) {        laucher.removeBasicPlayerListener(bpl);    }    /**     * Set SourceDataLine buffer size. It affects audio latency.     * (the delay between line.write(data) and real sound).     * Minimum value should be over 10000 bytes.     * @param size -1 means maximum buffer size available.     */    public void setLineBufferSize(int size) {        lineBufferSize = size;    }    /**     * Return SourceDataLine buffer size.     * @return -1 maximum buffer size.     */    public int getLineBufferSize() {        return lineBufferSize;    }    /**     * Return SourceDataLine current buffer size.     * @return     */    public int getLineCurrentBufferSize() {        return m_lineCurrentBufferSize;    }    /**     * Set thread sleep time.     * Default is -1 (no sleep time).     * @param time in milliseconds.     */    public void setSleepTime(long time) {        threadSleep = time;    }    /**     * Return thread sleep time in milliseconds.     * @return -1 means no sleep time.     */    public long getSleepTime() {        return threadSleep;    }    /**     * Returns BasicPlayer status.     * @return status     */    public int getStatus() {        return m_status;    }    /**     * Open file to play.     */    public void open(File file) throws BasicPlayerException {        log.info("open(" + file + ")");        if (file != null) {            m_dataSource = file;            initAudioInputStream();        }    }    /**     * Open URL to play.     */    public void open(URL url) throws BasicPlayerException {        log.info("open(" + url + ")");        if (url != null) {            m_dataSource = url;            initAudioInputStream();        }    }    /**     * Open inputstream to play.     */    public void open(InputStream inputStream) throws BasicPlayerException {        log.info("open(" + inputStream + ")");        if (inputStream != null) {            m_dataSource = inputStream;            initAudioInputStream();        }    }    /**     * Inits AudioInputStream and AudioFileFormat from the data source.     * @throws BasicPlayerException     */    protected void initAudioInputStream() throws BasicPlayerException {        try {            reset();            notifyEvent(BasicPlayerEvent.OPENING, getEncodedStreamPosition(), -1, m_dataSource);            if (m_dataSource instanceof URL) {                initAudioInputStream((URL) m_dataSource);            } else if (m_dataSource instanceof File) {                initAudioInputStream((File) m_dataSource);            } else if (m_dataSource instanceof InputStream) {                initAudioInputStream((InputStream) m_dataSource);            }            createLine();            // Notify listeners with AudioFileFormat properties.            Map properties = null;            if (m_audioFileFormat instanceof TAudioFileFormat) {                // Tritonus SPI compliant audio file format.                properties = ((TAudioFileFormat) m_audioFileFormat).properties();                // Clone the Map because it is not mutable.                properties = deepCopy(properties);            } else {                properties = new HashMap();            }            // Add JavaSound properties.            if (m_audioFileFormat.getByteLength() > 0) {                properties.put("audio.length.bytes", new Integer(m_audioFileFormat.getByteLength()));            }            if (m_audioFileFormat.getFrameLength() > 0) {                properties.put("audio.length.frames", new Integer(m_audioFileFormat.getFrameLength()));            }            if (m_audioFileFormat.getType() != null) {                properties.put("audio.type", (m_audioFileFormat.getType().toString()));            }            // Audio format.            AudioFormat audioFormat = m_audioFileFormat.getFormat();            if (audioFormat.getFrameRate() > 0) {                properties.put("audio.framerate.fps", new Float(audioFormat.getFrameRate()));            }            if (audioFormat.getFrameSize() > 0) {                properties.put("audio.framesize.bytes", new Integer(audioFormat.getFrameSize()));            }            if (audioFormat.getSampleRate() > 0) {                properties.put("audio.samplerate.hz", new Float(audioFormat.getSampleRate()));            }            if (audioFormat.getSampleSizeInBits() > 0) {                properties.put("audio.samplesize.bits", new Integer(audioFormat.getSampleSizeInBits()));            }            if (audioFormat.getChannels() > 0) {                properties.put("audio.channels", new Integer(audioFormat.getChannels()));            }            if (audioFormat instanceof TAudioFormat) {                // Tritonus SPI compliant audio format.                Map addproperties = ((TAudioFormat) audioFormat).properties();                properties.putAll(addproperties);            }            // Add SourceDataLine            properties.put("basicplayer.sourcedataline", m_line);            Iterator<BasicPlayerListener> it = laucher.getBasicPlayerListeners().iterator();            while (it.hasNext()) {                BasicPlayerListener bpl = it.next();                bpl.opened(m_dataSource, properties);            }            m_status = OPENED;            notifyEvent(BasicPlayerEvent.OPENED, getEncodedStreamPosition(), -1, null);        } catch (LineUnavailableException e) {            throw new BasicPlayerException(e);        } catch (UnsupportedAudioFileException e) {            throw new BasicPlayerException(e);        } catch (IOException e) {            throw new BasicPlayerException(e);        }    }    /**     * Inits Audio ressources from file.     */    protected void initAudioInputStream(File file) throws UnsupportedAudioFileException, IOException {        m_audioInputStream = AudioSystem.getAudioInputStream(file);        m_audioFileFormat = AudioSystem.getAudioFileFormat(file);    }    /**     * Inits Audio ressources from URL.     */    protected void initAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException {        m_audioInputStream = AudioSystem.getAudioInputStream(url);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -