📄 playercomponent.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package zdrive.engines;/** * * @author Mikhail *//* * To change this template, choose Tools | Templates * and open the template in the editor. */import java.io.IOException;import java.util.ArrayDeque;import java.util.Deque;import java.util.HashMap;import java.util.logging.Level;import java.util.logging.Logger;import javax.media.ControllerErrorEvent;import javax.media.ControllerEvent;import javax.media.ControllerListener;import javax.media.EndOfMediaEvent;import javax.media.NoPlayerException;import javax.media.Player;import javax.media.Manager;import javax.media.MediaLocator;import java.net.URL;import java.io.*;/** * * @author user */public class PlayerComponent implements ControllerListener { private Player current; private Deque<String> queue = new ArrayDeque<String>(); private HashMap<String, MediaLocator> sounds = new HashMap<String, MediaLocator>(); public synchronized void append (String path) { queue.add(path); } public synchronized void resume () { if (current == null) playFile(queue.pollFirst()); } public synchronized void stop () { queue.clear(); current.stop(); current = null; } private synchronized void fetchNext () { if (!queue.isEmpty()) playFile(queue.pollFirst()); else current = null; } public synchronized boolean isDone(){ return current==null; } private void addPath(String path) { String fullPath = null; try { fullPath = new File(path).getCanonicalPath(); } catch (IOException ex) { //Logger.getLogger(TestSound.class.getName()).log(Level.SEVERE, null, ex); } MediaLocator locator = null; locator = new MediaLocator("file:///" + fullPath); sounds.put(path, locator); } private synchronized void playFile (String path) { if (current != null) current.stop(); if (!sounds.containsKey(path)) addPath(path); try { current = Manager.createPlayer(sounds.get(path)); } catch (IOException ex) { //Logger.getLogger(Voice.class.getName()).log(Level.SEVERE, null, ex); } catch (NoPlayerException ex) { // Logger.getLogger(Voice.class.getName()).log(Level.SEVERE, null, ex); } current.addControllerListener(this); current.start(); } public synchronized void controllerUpdate(ControllerEvent event) { if (current == null) return; if (event.getSource() != current) return; if (event instanceof EndOfMediaEvent) { fetchNext(); } else if (event instanceof ControllerErrorEvent) { // System.out.println("Error while playing media"); stop(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -