Package ext

Source Code of ext.AsyncMp3Controller

package ext;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Logger;

import myplayer.IPlayerController;
import myplayer.Mp3InputStream;
import myplayer.Mp3PlayerController;
import myplayer.MyPlayer;
import myplayer.PlayerListener;
import myplayer.Status;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;


/**
* Implementazione di IPlayerController con metodi wait e notify.
* */
public class AsyncMp3Controller implements IPlayerController {
  public AsyncMp3Controller(String fileName) throws FileNotFoundException,
      JavaLayerException {
    player = new MyPlayer(new Mp3InputStream(fileName));
  }

  public void play() {
    if (stat == Status.INACTIVE) {
      synchronized (lock) {
        stat = Status.ACTIVE;
        new Thread(this).start();
      }
    } else if (stat == Status.PAUSED)
      resume();
  }

  public void run() {
    while (stat != Status.FINISHED) {
      try {
        player.play(1);
      } catch (JavaLayerException e) {
        e.printStackTrace();
        break;
      }
      synchronized (lock) {
        while (stat == Status.PAUSED) {
          try {
            logger.info("Fermato...");
            lock.wait();
            logger.info("Ripartito...");
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }
    logger.info("Finito: " + player.getPosition());
    player.close();
  }

  public void pause() {
    synchronized (lock) {
      logger.info("In Pause()");
      stat = Status.PAUSED;
      lock.notifyAll();
    }
  }

  public void stop() {
    synchronized (lock) {
      stat = Status.FINISHED;
      lock.notifyAll();
    }
  }

  @Override
  public void resume() {
    synchronized (lock) {
      if (stat != Status.FINISHED) {
        stat = Status.ACTIVE;
        lock.notifyAll();
      }
    }
  }

  public Status getStatus() {
    return stat;
  }

  @Override
  public void skip(int s) {
  }
 
  @Override
  public int getFramesNumber() {
    return player.getFrameNumber();
  }

  public void setListener(PlayerListener lst) {
    player.addListener(lst);
  }

  public int getPosition() {
    return player.getPosition();
  }

  private MyPlayer player;
  private Status stat = Status.INACTIVE;
  private Logger logger = Logger.getLogger("global");
  private final Object lock = new Object();
}
TOP

Related Classes of ext.AsyncMp3Controller

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.