栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JLayer-暂停并恢复歌曲

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JLayer-暂停并恢复歌曲

播放器的一个非常简单的实现,实际上是暂停播放。它通过使用单独的线程播放流并告诉播放器线程是否/何时暂停和继续工作来工作。

public class PausablePlayer {    private final static int NOTSTARTED = 0;    private final static int PLAYING = 1;    private final static int PAUSED = 2;    private final static int FINISHED = 3;    // the player actually doing all the work    private final Player player;    // locking object used to communicate with player thread    private final Object playerLock = new Object();    // status variable what player thread is doing/supposed to do    private int playerStatus = NOTSTARTED;    public PausablePlayer(final InputStream inputStream) throws JavaLayerException {        this.player = new Player(inputStream);    }    public PausablePlayer(final InputStream inputStream, final AudioDevice audioDevice) throws JavaLayerException {        this.player = new Player(inputStream, audioDevice);    }        public void play() throws JavaLayerException {        synchronized (playerLock) { switch (playerStatus) {     case NOTSTARTED:         final Runnable r = new Runnable() {  public void run() {      playInternal();  }         };         final Thread t = new Thread(r);         t.setDaemon(true);         t.setPriority(Thread.MAX_PRIORITY);         playerStatus = PLAYING;         t.start();         break;     case PAUSED:         resume();         break;     default:         break; }        }    }        public boolean pause() {        synchronized (playerLock) { if (playerStatus == PLAYING) {     playerStatus = PAUSED; } return playerStatus == PAUSED;        }    }        public boolean resume() {        synchronized (playerLock) { if (playerStatus == PAUSED) {     playerStatus = PLAYING;     playerLock.notifyAll(); } return playerStatus == PLAYING;        }    }        public void stop() {        synchronized (playerLock) { playerStatus = FINISHED; playerLock.notifyAll();        }    }    private void playInternal() {        while (playerStatus != FINISHED) { try {     if (!player.play(1)) {         break;     } } catch (final JavaLayerException e) {     break; } // check if paused or terminated synchronized (playerLock) {     while (playerStatus == PAUSED) {         try {  playerLock.wait();         } catch (final InterruptedException e) {  // terminate player  break;         }     } }        }        close();    }        public void close() {        synchronized (playerLock) { playerStatus = FINISHED;        }        try { player.close();        } catch (final Exception e) { // ignore, we are terminating anyway        }    }    // demo how to use    public static void main(String[] argv) {        try { FileInputStream input = new FileInputStream("myfile.mp3");  PausablePlayer player = new PausablePlayer(input); // start playing player.play(); // after 5 secs, pause Thread.sleep(5000); player.pause(); // after 5 secs, resume Thread.sleep(5000); player.resume();        } catch (final Exception e) { throw new RuntimeException(e);        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498352.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号