📄 basicplayer.java
字号:
m_line = null; } // Notification of "End Of Media" if (nBytesRead == -1) { notifyEvent(BasicPlayerEvent.EOM, getEncodedStreamPosition(), -1, null); } // Close stream. closeStream(); } m_status = STOPPED; notifyEvent(BasicPlayerEvent.STOPPED, getEncodedStreamPosition(), -1, null); log.info("Thread completed"); } /** * Skip bytes in the File inputstream. * It will skip N frames matching to bytes, so it will never skip given bytes length exactly. * @param bytes * @return value>0 for File and value=0 for URL and InputStream * @throws BasicPlayerException */ protected long skipBytes(long bytes) throws BasicPlayerException { long totalSkipped = 0; if (m_dataSource instanceof File) { log.fine("Bytes to skip : " + bytes); int previousStatus = m_status; m_status = SEEKING; long skipped = 0; try { synchronized (m_audioInputStream) { notifyEvent(BasicPlayerEvent.SEEKING, getEncodedStreamPosition(), -1, null); initAudioInputStream(); if (m_audioInputStream != null) { // Loop until bytes are really skipped. while (totalSkipped < (bytes - SKIP_INACCURACY_SIZE)) { skipped = m_audioInputStream.skip(bytes - totalSkipped); if (skipped == 0) { break; } totalSkipped = totalSkipped + skipped; log.fine("Skipped : " + totalSkipped + "/" + bytes); if (totalSkipped == -1) { throw new BasicPlayerException(BasicPlayerException.SKIPNOTSUPPORTED); } } } } notifyEvent(BasicPlayerEvent.SEEKED, getEncodedStreamPosition(), -1, null); m_status = OPENED; if (previousStatus == PLAYING) { startPlayback(); } else if (previousStatus == PAUSED) { startPlayback(); pausePlayback(); } } catch (IOException e) { throw new BasicPlayerException(e); } } return totalSkipped; } /** * Notify listeners about a BasicPlayerEvent. * @param code event code. * @param position in the stream when the event occurs. */ protected void notifyEvent(int code, int position, double value, Object description) { BasicPlayerEvent event = new BasicPlayerEvent(this, code, position, value, description); laucher.put(event); } protected int getEncodedStreamPosition() { int nEncodedBytes = -1; if (m_dataSource instanceof File) { try { if (m_encodedaudioInputStream != null) { nEncodedBytes = encodedLength - m_encodedaudioInputStream.available(); } } catch (IOException e) { //log.debug("Cannot get m_encodedaudioInputStream.available()",e); } } return nEncodedBytes; } protected void closeStream() { // Close stream. try { if (m_audioInputStream != null) { m_audioInputStream.close(); log.info("Stream closed"); } } catch (IOException e) { log.log(Level.SEVERE, "Cannot close stream", e); } } /** * Returns true if Gain control is supported. */ public boolean hasGainControl() { if (m_gainControl == null) { // Try to get Gain control again (to support J2SE 1.5) if ((m_line != null) && (m_line.isControlSupported(FloatControl.Type.MASTER_GAIN))) { m_gainControl = (FloatControl) m_line.getControl(FloatControl.Type.MASTER_GAIN); } } return m_gainControl != null; } /** * Returns Gain value. */ public float getGainValue() { if (hasGainControl()) { return m_gainControl.getValue(); } else { return 0.0F; } } /** * Gets max Gain value. */ public float getMaximumGain() { if (hasGainControl()) { return m_gainControl.getMaximum(); } else { return 0.0F; } } /** * Gets min Gain value. */ public float getMinimumGain() { if (hasGainControl()) { return m_gainControl.getMinimum(); } else { return 0.0F; } } /** * Returns true if Pan control is supported. */ public boolean hasPanControl() { if (m_panControl == null) { // Try to get Pan control again (to support J2SE 1.5) if ((m_line != null) && (m_line.isControlSupported(FloatControl.Type.PAN))) { m_panControl = (FloatControl) m_line.getControl(FloatControl.Type.PAN); } } return m_panControl != null; } /** * Returns Pan precision. */ public float getPrecision() { if (hasPanControl()) { return m_panControl.getPrecision(); } else { return 0.0F; } } /** * Returns Pan value. */ public float getPan() { if (hasPanControl()) { return m_panControl.getValue(); } else { return 0.0F; } } /** * Deep copy of a Map. * @param src * @return */ protected Map deepCopy(Map src) { HashMap map = new HashMap(); if (src != null) { Iterator it = src.keySet().iterator(); while (it.hasNext()) { Object key = it.next(); Object value = src.get(key); map.put(key, value); } } return map; } /** * @see javazoom.jlgui.basicplayer.BasicController#seek(long) */ public long seek(long bytes) throws BasicPlayerException { return skipBytes(bytes); } /** * @see javazoom.jlgui.basicplayer.BasicController#play() */ public void play() throws BasicPlayerException { startPlayback(); } /** * @see javazoom.jlgui.basicplayer.BasicController#stop() */ public void stop() throws BasicPlayerException { stopPlayback(); } /** * @see javazoom.jlgui.basicplayer.BasicController#pause() */ public void pause() throws BasicPlayerException { pausePlayback(); } /** * @see javazoom.jlgui.basicplayer.BasicController#resume() */ public void resume() throws BasicPlayerException { resumePlayback(); } /** * Sets Pan value. * Line should be opened before calling this method. * Linear scale : -1.0 <--> +1.0 */ public void setPan(double fPan) throws BasicPlayerException { if (hasPanControl()) { log.fine("Pan : " + fPan); m_panControl.setValue((float) fPan); notifyEvent(BasicPlayerEvent.PAN, getEncodedStreamPosition(), fPan, null); } else { throw new BasicPlayerException(BasicPlayerException.PANCONTROLNOTSUPPORTED); } } /** * Sets Gain value. * Line should be opened before calling this method. * Linear scale 0.0 <--> 1.0 * Threshold Coef. : 1/2 to avoid saturation. */ public void setGain(double fGain) throws BasicPlayerException { if (hasGainControl()) { double minGainDB = getMinimumGain(); double ampGainDB = ((10.0f / 20.0f) * getMaximumGain()) - getMinimumGain(); double cste = Math.log(10.0) / 20; double valueDB = minGainDB + (1 / cste) * Math.log(1 + (Math.exp(cste * ampGainDB) - 1) * fGain); log.finest("Gain : " + valueDB); m_gainControl.setValue((float) valueDB); notifyEvent(BasicPlayerEvent.GAIN, getEncodedStreamPosition(), fGain, null); } else { throw new BasicPlayerException(BasicPlayerException.GAINCONTROLNOTSUPPORTED); } } public List getMixers() { ArrayList mixers = new ArrayList(); Mixer.Info[] mInfos = AudioSystem.getMixerInfo(); if (mInfos != null) { for (int i = 0; i < mInfos.length; i++) { Line.Info lineInfo = new Line.Info(SourceDataLine.class); Mixer mixer = AudioSystem.getMixer(mInfos[i]); if (mixer.isLineSupported(lineInfo)) { mixers.add(mInfos[i].getName()); } } } return mixers; } public Mixer getMixer(String name) { Mixer mixer = null; if (name != null) { Mixer.Info[] mInfos = AudioSystem.getMixerInfo(); if (mInfos != null) { for (int i = 0; i < mInfos.length; i++) { if (mInfos[i].getName().equals(name)) { mixer = AudioSystem.getMixer(mInfos[i]); break; } } } } return mixer; } public String getMixerName() { return m_mixerName; } public void setMixerName(String name) { m_mixerName = name; } public long getMicrosecondPosition() { if (m_line != null) { return m_line.getMicrosecondPosition(); } else { return -1; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -