Android Open Source - EnklawaPlayer Player Manager






From Project

Back to project page EnklawaPlayer.

License

The source code is released under:

GNU General Public License

If you think the Android project EnklawaPlayer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package macbury.pod.managers.player;
//from   w  ww. jav  a2  s. co  m
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.PowerManager;
import android.util.Log;

import java.io.IOException;
import java.util.ArrayList;

import macbury.pod.db.models.EnqueueEpisode;
import macbury.pod.extensions.SleepTimer;
import macbury.pod.managers.App;
import macbury.pod.managers.player.sources.AbstractMediaSource;
import macbury.pod.managers.player.sources.EpisodeMediaSource;
import macbury.pod.managers.player.sources.RadioMediaSource;

/**
 * Created by macbury on 19.09.14.
 */
public class PlayerManager implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener, SleepTimer.SleepTimerListener, MediaPlayer.OnInfoListener {
  private static final String TAG = "PlayerManager";
  private final SleepTimer fakeLoop;
  private AbstractMediaSource currentMediaSource;
  private final Context context;
  private final MediaPlayer player;
  private boolean preparing;
  private ArrayList<PlayerManagerListener> listeners;
  private boolean playAfterPrepare = true;

  public PlayerManager(Context context) {
    this.context   = context;
    this.fakeLoop  = new SleepTimer(1000, this);
    this.player    = new MediaPlayer();
    this.listeners = new ArrayList<PlayerManagerListener>();

    player.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK);
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    player.setOnBufferingUpdateListener(this);
    player.setOnPreparedListener(this);
    player.setOnCompletionListener(this);
    player.setOnErrorListener(this);
    player.setOnInfoListener(this);
    fakeLoop.start();
  }

  public void start() {
    if (isRunning()) {
      play();
    } else {
      next();
    }
  }

  public PlaybackStatus getState() {
    if (isRunning()) {
      return currentMediaSource.getStatus();
    } else {
      return PlaybackStatus.Pending;
    }
  }

  public void addListener(PlayerManagerListener listener) {
    if (listeners.indexOf(listener) == -1) {
      listeners.add(listener);
    }
  }

  public void removeListener(PlayerManagerListener listener) {
    if (listeners.indexOf(listener) != -1) {
      listeners.remove(listener);
    }
  }

  private void next() {
    EnqueueEpisode nextToPlay = App.current().db.queue.nextToPlay();
    if (nextToPlay == null) {
      exit();
    } else {
      set(new EpisodeMediaSource(nextToPlay));
    }
  }

  public void set(AbstractMediaSource mediaSource) {
    currentMediaSource = mediaSource;
    preparing          = true;
    try {
      Log.i(TAG, "Episode " + currentMediaSource.getTitle() + " is " + currentMediaSource.getMediaUri().toString());
      for (PlayerManagerListener listener : listeners) {
        listener.onInitialize(this, currentMediaSource);
      }
      player.reset();
      player.setDataSource(context, currentMediaSource.getMediaUri());
      player.prepareAsync();
    } catch (IOException e) {
      e.printStackTrace();
      for (PlayerManagerListener listener : listeners) {
        listener.onMediaError(this, MediaPlayer.MEDIA_ERROR_IO);
      }
      exit();
    }
  }

  public boolean isRunning() {
    return currentMediaSource != null;
  }

  public void destroy() {
    fakeLoop.kill();
    player.stop();
    player.release();
  }


  public void restart() {
    if (isRunning()) {
      pause();
    }
    next();
  }

  public void cancel() {
    stop();
    exit();
  }

  public void pauseAndExit() {
    pause();
    exit();
  }

  public void exit() {
    Log.i(TAG, "Exiting playlist");
    for (PlayerManagerListener listener : listeners) {
      listener.onFinishAll(this);
    }
  }

  public void play() {
    if (!preparing || !player.isPlaying()) {
      Log.i(TAG, "Play");
      currentMediaSource.onPlay();
      player.start();

      for (PlayerManagerListener listener : listeners) {
        listener.onPlay(this, currentMediaSource);
      }
    }
  }

  public void pause() {
    if (isPlaying()) {
      Log.i(TAG, "pause");
      currentMediaSource.onPause();
      player.pause();

      for (PlayerManagerListener listener : listeners) {
        listener.onPause(this, currentMediaSource);
      }
    }
  }

  public void stop() {
    if (isPlaying()) {
      Log.i(TAG, "stop");
      player.stop();
      for (PlayerManagerListener listener : listeners) {
        listener.onFinish(this, currentMediaSource);
      }
      finishPlayback();
    }
  }

  private void finishPlayback() {
    player.reset();
    currentMediaSource.onFinishPlayback();
    currentMediaSource = null;
  }

  @Override
  public void onPrepared(MediaPlayer mp) {
    Log.i(TAG, "onPrepared seeking to: " + currentMediaSource.getPosition());
    preparing = false;
    seekTo(currentMediaSource.getPosition());
    if (playAfterPrepare)
      play();
  }

  @Override
  public void onBufferingUpdate(MediaPlayer mp, int percent) {
    for (PlayerManagerListener listener : listeners) {
      listener.onBufferMedia(this, currentMediaSource);
    }
  }

  @Override
  public void onCompletion(MediaPlayer mp) {
    finishPlayback();
    next();
  }

  @Override
  public boolean onError(MediaPlayer mp, int what, int extra) {
    Log.i(TAG, "Recived error: " +extra);
    pause();
    for (PlayerManagerListener listener : listeners) {
      listener.onMediaError(this, extra);
    }
    return true;
  }

  @Override
  public void onSleepTimerTick(SleepTimer timer) {
    if (isPlaying()) {
      updatePosition();
    }
  }

  private void updatePosition() {
    currentMediaSource.setPosition(player.getCurrentPosition());
    for (PlayerManagerListener listener : listeners) {
      listener.onMediaUpdate(this, currentMediaSource);
    }
  }

  @Override
  public boolean onInfo(MediaPlayer mp, int what, int extra) {
    Log.i(TAG, "what info= "+what);
    return false;
  }

  public AbstractMediaSource getCurrentMediaSource() {
    return currentMediaSource;
  }

  public boolean isPlaying() {
    return isRunning() && player.isPlaying();
  }

  public boolean isPreparing() {
    return preparing;
  }

  public boolean isRunning(EnqueueEpisode enqueueEpisode) {
    return isRunning() && currentMediaSource.equals(enqueueEpisode);
  }

  public boolean isRunning(RadioMediaSource rms) {
    return isRunning() && currentMediaSource.equals(rms);
  }

  public void seekTo(int duration) {
    if (isRunning()) {
      Log.i(TAG, "Seek to "+ duration);
      player.seekTo(duration);
      updatePosition();
    }
  }

  public void lowerVolume() {
    player.setVolume(0.5f, 0.5f);
  }

  public void normalVolume() {
    player.setVolume(1,1);
  }

  public boolean isPaused() {
    return !isPlaying();
  }

  public void setPlayAfterPrepare(boolean playAfterPrepare) {
    this.playAfterPrepare = playAfterPrepare;
  }

  public boolean isPlayAfterPrepare() {
    return playAfterPrepare;
  }


}




Java Source Code List

macbury.pod.ApplicationTest.java
macbury.pod.activities.DonateActivity.java
macbury.pod.activities.ProgramEpisodesActivity.java
macbury.pod.activities.SettingsActivity.java
macbury.pod.activities.ext.AccentFragmentActivity.java
macbury.pod.activities.main.MainActivityActionBarToggle.java
macbury.pod.activities.main.MainActivity.java
macbury.pod.activities.main.NavigationController.java
macbury.pod.activities.main.NavigationListener.java
macbury.pod.activities.player.PlayerActivity.java
macbury.pod.activities.player.RadioActivity.java
macbury.pod.adapters.EpisodesAdapterListener.java
macbury.pod.adapters.EpisodesAdapter.java
macbury.pod.adapters.ProgramsAdapter.java
macbury.pod.api.APICategory.java
macbury.pod.api.APIEpisode.java
macbury.pod.api.APIProgram.java
macbury.pod.api.APIResponse.java
macbury.pod.api.APIThread.java
macbury.pod.db.DatabaseCRUDListener.java
macbury.pod.db.models.EnqueueEpisode.java
macbury.pod.db.models.EpisodeFile.java
macbury.pod.db.models.Episode.java
macbury.pod.db.models.ForumThread.java
macbury.pod.db.models.Program.java
macbury.pod.db.scopes.AbstractScope.java
macbury.pod.db.scopes.EnqueueEpisodeScope.java
macbury.pod.db.scopes.EpisodeFilesScope.java
macbury.pod.db.scopes.EpisodesScope.java
macbury.pod.db.scopes.ProgramsScope.java
macbury.pod.db.scopes.ThreadScope.java
macbury.pod.dialogs.EpisodeAboutDialog.java
macbury.pod.dialogs.ProgramAboutDialog.java
macbury.pod.extensions.Converter.java
macbury.pod.extensions.DateDeserializer.java
macbury.pod.extensions.SleepTimer.java
macbury.pod.fragments.SettingsFragment.java
macbury.pod.fragments.main.AbstractEpisodesFragment.java
macbury.pod.fragments.main.AllProgramsFragment.java
macbury.pod.fragments.main.EnklawaBaseAbstractListFragment.java
macbury.pod.fragments.main.episodes.DownloadedEpisodesFragment.java
macbury.pod.fragments.main.episodes.NewestEpisodesFragment.java
macbury.pod.fragments.main.episodes.PlaylistFragment.java
macbury.pod.fragments.main.episodes.ProgramEpisodesFragment.java
macbury.pod.fragments.player.ExternalPlayerFragment.java
macbury.pod.fragments.player.PlayerArtworkAndInfoFragment.java
macbury.pod.fragments.player.PlayerControllerFragment.java
macbury.pod.managers.AlarmsManager.java
macbury.pod.managers.App.java
macbury.pod.managers.BroadcastsManager.java
macbury.pod.managers.DatabaseManager.java
macbury.pod.managers.IntentManager.java
macbury.pod.managers.NotificationsManager.java
macbury.pod.managers.ServiceManager.java
macbury.pod.managers.SettingsManager.java
macbury.pod.managers.StorageManager.java
macbury.pod.managers.download.DownloadEpisode.java
macbury.pod.managers.download.DownloadManagerListener.java
macbury.pod.managers.download.DownloadManager.java
macbury.pod.managers.player.PlaybackStatus.java
macbury.pod.managers.player.PlayerManagerListener.java
macbury.pod.managers.player.PlayerManager.java
macbury.pod.managers.player.sources.AbstractMediaSource.java
macbury.pod.managers.player.sources.EpisodeMediaSource.java
macbury.pod.managers.player.sources.RadioMediaSource.java
macbury.pod.navigation_drawer.NavAdapter.java
macbury.pod.navigation_drawer.NavBaseItem.java
macbury.pod.navigation_drawer.NavDivider.java
macbury.pod.navigation_drawer.items.AllProgramsNavItem.java
macbury.pod.navigation_drawer.items.DownloadedEpisodesNavItem.java
macbury.pod.navigation_drawer.items.FavoriteProgramNavItem.java
macbury.pod.navigation_drawer.items.ForumNavItem.java
macbury.pod.navigation_drawer.items.NavItemFragment.java
macbury.pod.navigation_drawer.items.NewestEpisodesNavItem.java
macbury.pod.navigation_drawer.items.PlayQueueNavItem.java
macbury.pod.receivers.BootReceiver.java
macbury.pod.receivers.MediaButtonReceiver.java
macbury.pod.services.DownloadService.java
macbury.pod.services.PlayerService.java
macbury.pod.services.SyncPodService.java
macbury.pod.tutorial.MainActivityTutorial.java
macbury.pod.views.TappableFrameLayout.java