Package org.livesub.player

Source Code of org.livesub.player.Player

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.livesub.player;

import java.io.File;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javafx.stage.FileChooser;
import org.apache.log4j.Logger;
import org.livesub.input.Caption;
import org.livesub.input.CaptionsParser;
import org.livesub.ui.MainUIController;
import org.livesub.utils.Utils;
import org.livesub.utils.doublelinkedlist.DoubleLinkedList;

/**
*
* @author vasilis
*/
public class Player {

    private static final Logger LOG = Logger.getLogger(Player.class);
   
    MainUIController controller;
    DoubleLinkedList<Caption> subtitles;
    private final ScheduledThreadPoolExecutor playTimer;
    private ScheduledFuture<?> futureTimer;
    private final SubtitleTask subtitleTask;
   
    public Player(MainUIController controller) {
        this.controller = controller;
        this.playTimer = new ScheduledThreadPoolExecutor(Utils.SRT_DELAY);
        this.futureTimer = null;
        subtitleTask = new SubtitleTask(this);
    }

    public File importSubtitles() {
        File file = new FileChooser().showOpenDialog(null);
        return file;
    }

    public void loadSubtitles() {
        loadSubtitles(importSubtitles());
    }

    public void loadSubtitles(File file) {
        if (file == null) {
            LOG.warn("Can't load null file!");
            return;
        }
       
        LOG.info(String.format("Load file %s", file.getPath()));
       
        stop();
        CaptionsParser parser = new CaptionsParser();
        subtitles = parser.read(file);
        subtitleTask.setSubtitleList(subtitles);
        setDisplayedText(Utils.LOADED_TEXT);
        Utils.playerState.setState(PlayerStateEnum.LOADED);

    }

    public void goTo(int captionKey){
        LOG.trace(String.format("Go to line: %d", captionKey));
        subtitleTask.goTo(captionKey);
    }
   
    public void jumpForward(){
        subtitleTask.jump(1);
    }
   
    public void jumpBackward(){
        subtitleTask.jump(-1);
    }
   
    public void play() {
        this.futureTimer = this.playTimer.scheduleAtFixedRate(subtitleTask, 0, Utils.SRT_DELAY, TimeUnit.MILLISECONDS);
       
        if (getDisplayedText().contains(Utils.PAUSED_TEXT)) {
            setDisplayedText(getDisplayedText().substring(Utils.PAUSED_TEXT.length()));
        }
       
        Utils.playerState.setState(PlayerStateEnum.PLAYING);
    }

    public void pause() {
        playTimer.remove(subtitleTask);
        if(this.futureTimer != null){
            this.futureTimer.cancel(true);
        }
       
        setDisplayedText(Utils.PAUSED_TEXT + getDisplayedText());
       
        Utils.playerState.setState(PlayerStateEnum.PAUSED);
    }
   
    public void stop() {
        pause();
       
        Utils.playerState.setState(PlayerStateEnum.IDLE);
       
        subtitleTask.reset();
    }
   
    private String getDisplayedText() {
        return controller.getDisplayedText();
    }
   
    public void setDisplayedText(String text) {
        if(text == null) {
            text = "";
        }
        controller.setDisplayedText(text);
    }
   
    public DoubleLinkedList<Caption> getSubtitleList(){
        return subtitles;
    }
}
TOP

Related Classes of org.livesub.player.Player

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.