Package org.jampa.engine.jlayer

Source Code of org.jampa.engine.jlayer.JLayerEngine

/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*/

package org.jampa.engine.jlayer;

import java.io.File;

import javazoom.jlgui.basicplayer.BasicPlayer;
import javazoom.jlgui.basicplayer.BasicPlayerException;

import org.jampa.controllers.Controller;
import org.jampa.engine.IEngine;
import org.jampa.engine.PlaybackEngine;
import org.jampa.logging.Log;
import org.jampa.model.IAudioItem;

public class JLayerEngine implements IEngine {

  private BasicPlayer _player;
  private JLayerEngineListener _listener;
 
  private int _durationInBytes;
  private int _durationInMilliSeconds;
 
  private long _ellapsedBytes;
 
  private int _playerVolume;
 
  private boolean _isMuted;
 
  public JLayerEngine(int playerVolume) {
    _player = new BasicPlayer();
    _listener = new JLayerEngineListener(this);
    _player.addBasicPlayerListener(_listener);
   
    _playerVolume = playerVolume;
   
    Log.getInstance(JLayerEngine.class).debug("JLayer engine initialized."); //$NON-NLS-1$
  }
 
  @Override
  public boolean isPaused() {
    return (_player.getStatus() == BasicPlayer.PAUSED);
  }

  @Override
  public boolean isPlaying() {
    // From Jampa's point of view, paused also means playing...
    return ((_player.getStatus() == BasicPlayer.PLAYING) || (_player.getStatus() == BasicPlayer.PAUSED));
  }

  @Override
  public void pausePlayback() {
    try {
      Log.getInstance(JLayerEngine.class).debug("Pause playback."); //$NON-NLS-1$
     
      if (isPaused()) {
        _player.resume();
      } else {
        _player.pause();
      }
    } catch (BasicPlayerException e) {
      Log.getInstance(JLayerEngine.class).warn("Error while pausing: " + e.getMessage());
    }   
  }

  @Override
  public void playFile(IAudioItem item) {
    try {
     
      Log.getInstance(JLayerEngine.class).debug("Start playing : " + item.getFileName()); //$NON-NLS-1$
     
      if (_player.getStatus() == BasicPlayer.PLAYING) {
        _player.stop();
      }
     
      _durationInBytes = 0;
      _durationInMilliSeconds = 0;
      _ellapsedBytes = 0;
     
      _isMuted = false;
     
      _player.open(new File(item.getFileName()));
      _player.play();
     
      setVolume(_playerVolume);
     
    } catch (BasicPlayerException e) {
      Log.getInstance(JLayerEngine.class).warn("Error while playing: " + e.getMessage());
    }       
  }

  @Override
  public void seek(int seconds) {
    try {
      Log.getInstance(JLayerEngine.class).debug("Seek: " + Integer.toString(seconds)); //$NON-NLS-1$
     
      long byteSeeked = (long) (_durationInBytes * ((seconds * 1000.0) / _durationInMilliSeconds));
     
      _player.seek(_ellapsedBytes + byteSeeked);
      _ellapsedBytes = _ellapsedBytes + byteSeeked;
      setVolume(_playerVolume);
     
    } catch (BasicPlayerException e) {
      Log.getInstance(JLayerEngine.class).warn("Error while seeking: " + e.getMessage());
    }   
  }

  @Override
  public void setEnginePath(String path) {
    // Nothing to do with JLayer.   
  }

  @Override
  public void setPosition(int position) { 
    Log.getInstance(JLayerEngine.class).debug("Set position : " + Integer.toString(position)); //$NON-NLS-1$
    long byteSeeked = (long) (_durationInBytes * ((position * 1000.0) / _durationInMilliSeconds));
    try {
     
      _player.seek(byteSeeked);
      _ellapsedBytes = byteSeeked;
      setVolume(_playerVolume);
     
    } catch (BasicPlayerException e) {
      Log.getInstance(JLayerEngine.class).warn("Error while setting position: " + e.getMessage());
    }
  }
 
  private void internalSetVolume(int value) {
    try {
      _player.setGain((double) value / 100);
    } catch (BasicPlayerException e) {
      Log.getInstance(JLayerEngine.class).warn("Error while setting volume: " + e.getMessage());
    }
  }

  @Override
  public void setVolume(int value) {
    _playerVolume = value;
    Log.getInstance(JLayerEngine.class).debug("Set volume : " + Integer.toString(_playerVolume)); //$NON-NLS-1$
    internalSetVolume(value);
  }

  @Override
  public void stopPlayback() {
    try {
      Log.getInstance(JLayerEngine.class).debug("Stop playback"); //$NON-NLS-1$
      _player.stop();                             
    } catch (BasicPlayerException e) {
      Log.getInstance(JLayerEngine.class).warn("Error while stopping: " + e.getMessage());
    }
   
  }

  @Override
  public void toggleMute() {
    Log.getInstance(JLayerEngine.class).debug("Muting."); //$NON-NLS-1$
    if (_isMuted) {
      internalSetVolume(_playerVolume);
      _isMuted = false;
    } else {
      internalSetVolume(0);
      _isMuted = true;
    }   
  }
 
  public boolean isMuted() {
    return _isMuted;
  }
 
  protected void notifyEndOfFile() { 
    Controller.getInstance().getPlaylistController().playNextInPlaylist(true);
  }
 
  protected void setDuration(Integer durationInBytes, int durationInSeconds) {
    _durationInBytes = durationInBytes.intValue();
    _durationInMilliSeconds = durationInSeconds;
  }
 
  protected void setEllapsedBytes(int ellapsedBytes) {
    _ellapsedBytes = ellapsedBytes;
  }
 
  public PlaybackEngine getEngineType() {
    return PlaybackEngine.JLAYER;
  }

}
TOP

Related Classes of org.jampa.engine.jlayer.JLayerEngine

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.