Android Open Source - EnklawaPlayer Player Controller Fragment






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.fragments.player;
/*from  ww w . j  a v  a2  s  . c  o  m*/
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.app.Fragment;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import at.markushi.ui.CircleButton;
import macbury.pod.R;
import macbury.pod.extensions.Converter;
import macbury.pod.managers.App;
import macbury.pod.managers.player.PlayerManager;
import macbury.pod.managers.player.PlayerManagerListener;
import macbury.pod.managers.player.sources.AbstractMediaSource;
import macbury.pod.services.PlayerService;

public class PlayerControllerFragment extends Fragment implements PlayerManagerListener, View.OnClickListener, SeekBar.OnSeekBarChangeListener {
  private static final String TAG = "PlayerControllerFragment";
  private PlayerService.PlayerBinder playerBinder;
  private SeekBar timeSeekBar;
  private TextView runningTextView;
  private TextView totalRunnigTextView;
  private CircleButton playPauseButton;
  private ProgressBar loadingProgress;
  private boolean seekingStarted;
  private boolean beforeSeekingPlaying;

  public PlayerControllerFragment() {
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view           = inflater.inflate(R.layout.fragment_player_controller, container, false);
    timeSeekBar         = (SeekBar)view.findViewById(R.id.seek_bar_player);
    runningTextView     = (TextView)view.findViewById(R.id.text_running_time);
    totalRunnigTextView = (TextView)view.findViewById(R.id.text_running_total_time);
    playPauseButton     = (CircleButton)view.findViewById(R.id.button_play_pause);
    loadingProgress     = (ProgressBar)view.findViewById(R.id.progress_loading);

    playPauseButton.setOnClickListener(this);
    timeSeekBar.setOnSeekBarChangeListener(this);
    return view;
  }

  @Override
  public void onResume() {
    super.onResume();
    getActivity().bindService(App.current().intents.player(), playerManagerServiceConnection, Context.BIND_AUTO_CREATE);
  }

  @Override
  public void onPause() {
    super.onPause();
    getActivity().unbindService(playerManagerServiceConnection);
    if (playerBinder != null) {
      Log.i(TAG, "Removing binder");
      playerBinder.removeListener(this);
      playerBinder = null;
    }
  }

  private ServiceConnection playerManagerServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
      PlayerControllerFragment.this.playerBinder = (PlayerService.PlayerBinder)service;
      playerBinder.addListener(PlayerControllerFragment.this);
      updateUIForCurrentMediaSource();
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
      Log.i(TAG, "Player service disconnected!");
      playerBinder.removeListener(PlayerControllerFragment.this);
      playerBinder = null;
    }
  };

  private void updateUIForCurrentMediaSource() {
    if (playerBinder != null) {
      PlayerManager pm = playerBinder.getPlayerManager();
      if (pm.isRunning())
        updateUIInfoForMediaSource(pm, pm.getCurrentMediaSource());
    }
  }

  private void updateUIInfoForMediaSource(PlayerManager playerManager, AbstractMediaSource mediaSource) {
    timeSeekBar.setMax(mediaSource.getDuration());
    if (!seekingStarted) {
      timeSeekBar.setProgress(mediaSource.getPosition());
      runningTextView.setText(Converter.getDurationStringLong(mediaSource.getPosition()));
    }

    totalRunnigTextView.setText(Converter.getDurationStringLong(mediaSource.getDuration()));

    if (playerManager.isPreparing()) {
      loadingProgress.setVisibility(View.VISIBLE);
      playPauseButton.setVisibility(View.GONE);
    } else {
      loadingProgress.setVisibility(View.GONE);
      playPauseButton.setVisibility(View.VISIBLE);
    }

    if (playerManager.isPlaying()) {
      playPauseButton.setImageResource(R.drawable.ic_action_av_pause);
    } else {
      playPauseButton.setImageResource(R.drawable.ic_action_av_play);
    }
  }

  @Override
  public void onInitialize(PlayerManager manager, AbstractMediaSource mediaSource) {
    updateUIInfoForMediaSource(manager, mediaSource);
  }

  @Override
  public void onPlay(PlayerManager manager, AbstractMediaSource mediaSource) {
    Log.d(TAG, "onPlay:" + mediaSource.getTitle());
    updateUIInfoForMediaSource(manager, mediaSource);
  }

  @Override
  public void onBufferMedia(PlayerManager manager, AbstractMediaSource mediaSource) {

  }

  @Override
  public void onMediaUpdate(PlayerManager playerManager, AbstractMediaSource mediaSource) {
    //Log.d(TAG, "onMediaUpdate:" + mediaSource.getDuration());
    updateUIInfoForMediaSource(playerManager, mediaSource);
  }

  @Override
  public void onMediaError(PlayerManager playerManager, int extra) {

  }

  @Override
  public void onPause(PlayerManager manager, AbstractMediaSource mediaSource) {
    Log.d(TAG, "onPause:" + mediaSource.getTitle());
    updateUIInfoForMediaSource(manager, mediaSource);
  }

  @Override
  public void onFinish(PlayerManager manager, AbstractMediaSource mediaSource) {
    Log.d(TAG, "onFinish:" + mediaSource.getTitle());
    updateUIInfoForMediaSource(manager, mediaSource);
  }

  @Override
  public void onFinishAll(PlayerManager manager) {
    Log.d(TAG, "onFinishAll:");
  }

  @Override
  public void onClick(View v) {
    if (v == playPauseButton && playerBinder != null) {
      PlayerManager pm = playerBinder.getPlayerManager();
      if (pm.isPlaying()) {
        pm.pause();
      } else {
        pm.play();
      }
    }
  }

  @Override
  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if (seekingStarted) {
      runningTextView.setText(Converter.getDurationStringLong(progress));
    }
  }

  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
    seekingStarted       = true;
    beforeSeekingPlaying = playerBinder.getPlayerManager().isPlaying();
    playerBinder.getPlayerManager().pause();
  }

  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
    PlayerManager pm = playerBinder.getPlayerManager();
    pm.seekTo(seekBar.getProgress());
    if (beforeSeekingPlaying)
      pm.play();
    beforeSeekingPlaying = false;
    seekingStarted       = false;
  }

  public void stopPlayerIfPaused() {
    if (playerBinder != null && playerBinder.getPlayerManager().isPaused()) {
      getActivity().startService(App.current().intents.stopPlayer());
    }
  }
}




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