Skip to content

Commit

Permalink
Removed the videoView and replacced it with surfaceView with the requ…
Browse files Browse the repository at this point in the history
…ired coding tweaks.

Added the vlc basic controls layout for the Android TV.

Implemented the rewind and forward button in tvplayer.

Added seek bar and some other improvements.

Reformatted the code.

Removed lint issues.

Removed the depriciated overlay fragment.

Removed the .avi crash.
  • Loading branch information
octacode committed Jul 16, 2017
1 parent 7e1822a commit c94ae8b
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,168 +20,59 @@
package org.amahi.anywhere.tv.activity;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
import android.widget.VideoView;

import org.amahi.anywhere.AmahiApplication;
import org.amahi.anywhere.R;
import org.amahi.anywhere.server.client.ServerClient;
import org.amahi.anywhere.server.model.ServerFile;
import org.amahi.anywhere.server.model.ServerShare;
import org.amahi.anywhere.tv.fragment.TvPlaybackAudioFragment;
import org.amahi.anywhere.tv.fragment.TvPlaybackVideoFragment;
import org.amahi.anywhere.util.Intents;
import org.amahi.anywhere.util.Time;

import java.util.ArrayList;

import javax.inject.Inject;

public class TvPlaybackVideoActivity extends Activity {

private VideoView mVideoView;

private LeanbackPlaybackState mPlaybackState = LeanbackPlaybackState.PLAYING;

private int mPosition = 0;
private long mStartTimeMillis;
private long mDuration = -1;

private enum LeanbackPlaybackState {
PLAYING, PAUSED, IDLE
}

@Inject
ServerClient serverClient;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tv_video_playback);
setUpInjections();
setContentView(R.layout.activity_tv_playback_overlay);
loadViews();
getFragmentManager().beginTransaction().replace(R.id.playback_controls_fragment_container, buildAudioFragment()).commit();
}

private void setUpInjections() {
AmahiApplication.from(this).inject(this);
}

private void loadViews(){
mVideoView = (VideoView) findViewById(R.id.videoView);
mVideoView.setFocusable(false);
mVideoView.setFocusableInTouchMode(false);
setVideoPath(getVideoUri().toString());
}

public void setVideoPath(String videoUrl) {
setPosition(0);
mVideoView.setVideoPath(videoUrl);
mStartTimeMillis = 0;
mDuration = Time.getDuration(videoUrl);
mVideoView.start();
}

private void setPosition(int position) {
if (position > mDuration) {
mPosition = (int) mDuration;
} else if (position < 0) {
mPosition = 0;
mStartTimeMillis = System.currentTimeMillis();
} else {
mPosition = position;
}
mStartTimeMillis = System.currentTimeMillis();
}

@Override
public void onDestroy() {
super.onDestroy();
stopPlayback();
mVideoView.suspend();
mVideoView.setVideoURI(null);
}

private void stopPlayback() {
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}

public void playPause(boolean doPlay) {
if (mPlaybackState == LeanbackPlaybackState.IDLE) {
setupCallbacks();
}

if (doPlay && mPlaybackState != LeanbackPlaybackState.PLAYING) {
mPlaybackState = LeanbackPlaybackState.PLAYING;
if (mPosition > 0) {
mVideoView.seekTo(mPosition);
}
mVideoView.start();
mStartTimeMillis = System.currentTimeMillis();
} else {
mPlaybackState = LeanbackPlaybackState.PAUSED;
int timeElapsedSinceStart = (int) (System.currentTimeMillis() - mStartTimeMillis);
setPosition(mPosition + timeElapsedSinceStart);
mVideoView.pause();
}
}

public int getPosition() {
return mPosition;
}

private void setupCallbacks() {

mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
mVideoView.stopPlayback();
mPlaybackState = LeanbackPlaybackState.IDLE;
return false;
}
});

mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mPlaybackState == LeanbackPlaybackState.PLAYING) {
mVideoView.start();
}
}
});

mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mPlaybackState = LeanbackPlaybackState.IDLE;
}
});
}

public void fastForward() {
if (mDuration != -1) {
setPosition(mVideoView.getCurrentPosition() + (10 * 1000));
mVideoView.seekTo(mPosition);
}
}

public void rewind() {
setPosition(mVideoView.getCurrentPosition() - (10 * 1000));
mVideoView.seekTo(mPosition);
private Fragment buildAudioFragment(){
Fragment fragment = new TvPlaybackVideoFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(Intents.Extras.SERVER_SHARE,getShare());
bundle.putParcelable(Intents.Extras.SERVER_FILE,getFile());
bundle.putParcelableArrayList(Intents.Extras.SERVER_FILES,getFiles());
fragment.setArguments(bundle);
return fragment;
}

private ServerShare getVideoShare() {
private ServerShare getShare(){
return getIntent().getParcelableExtra(Intents.Extras.SERVER_SHARE);
}

private ServerFile getVideoFile() {
private ServerFile getFile(){
return getIntent().getParcelableExtra(Intents.Extras.SERVER_FILE);
}

private Uri getVideoUri() {
return serverClient.getFileUri(getVideoShare(), getVideoFile());
private ArrayList<ServerFile> getFiles(){
return getIntent().getParcelableArrayListExtra(Intents.Extras.SERVER_FILES);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import android.media.MediaPlayer;
import android.media.session.PlaybackState;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v17.leanback.app.PlaybackOverlayFragment;
import android.support.annotation.RequiresApi;
import android.support.v17.leanback.app.PlaybackFragment;
import android.support.v17.leanback.widget.Action;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.ClassPresenterSelector;
Expand Down Expand Up @@ -59,7 +61,6 @@
import org.amahi.anywhere.task.AudioMetadataRetrievingTask;
import org.amahi.anywhere.tv.presenter.AudioDetailsDescriptionPresenter;
import org.amahi.anywhere.tv.presenter.MainTVPresenter;
import org.amahi.anywhere.tv.presenter.VideoDetailsDescriptionPresenter;
import org.amahi.anywhere.util.Intents;
import org.amahi.anywhere.util.Mimes;

Expand All @@ -68,11 +69,12 @@

import javax.inject.Inject;

public class TvPlaybackAudioFragment extends PlaybackOverlayFragment {
public class TvPlaybackAudioFragment extends PlaybackFragment {

private static final int DEFAULT_UPDATE_PERIOD = 1000;
private static final int UPDATE_PERIOD = 16;
@Inject
ServerClient serverClient;

private ArrayObjectAdapter mRowsAdapter;
private PlaybackControlsRow mPlaybackControlsRow;
private ArrayObjectAdapter mPrimaryActionsAdapter;
Expand All @@ -81,9 +83,6 @@ public class TvPlaybackAudioFragment extends PlaybackOverlayFragment {
private PlaybackControlsRow.SkipPreviousAction mSkipPreviousAction;
private PlaybackControlsRow.FastForwardAction mFastForwardAction;
private PlaybackControlsRow.RewindAction mRewindAction;

private static final int DEFAULT_UPDATE_PERIOD = 1000;
private static final int UPDATE_PERIOD = 16;
private int mCurrentPlaybackState;

private Handler mHandler;
Expand Down Expand Up @@ -164,6 +163,7 @@ private void setUpRows() {
playbackControlsRowPresenter.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.primary));
playbackControlsRowPresenter.setProgressColor(Color.WHITE);
playbackControlsRowPresenter.setOnActionClickedListener(new OnActionClickedListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onActionClicked(Action action) {
if (action.getId() == mPlayPauseAction.getId()) {
Expand All @@ -185,6 +185,7 @@ public void onActionClicked(Action action) {
setAdapter(mRowsAdapter);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void togglePlayPause(boolean isPaused) {
if (isPaused) {
mediaPlayer.pause();
Expand Down Expand Up @@ -224,6 +225,7 @@ private void skipPrevious() {
replaceFragment(getAudioFiles().get(getAudioFiles().size() - 1));
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void playbackStateChanged() {

if (mCurrentPlaybackState != PlaybackState.STATE_PLAYING) {
Expand All @@ -233,7 +235,7 @@ public void playbackStateChanged() {
mPlayPauseAction.setIndex(PlaybackControlsRow.PlayPauseAction.PAUSE);
mPlayPauseAction.setIcon(mPlayPauseAction.getDrawable(PlaybackControlsRow.PlayPauseAction.PAUSE));
notifyChanged(mPlayPauseAction);
} else if (mCurrentPlaybackState != PlaybackState.STATE_PAUSED) {
} else {
mCurrentPlaybackState = PlaybackState.STATE_PAUSED;
stopProgressAutomation();
mPlayPauseAction.setIndex(PlaybackControlsRow.PlayPauseAction.PLAY);
Expand Down Expand Up @@ -284,6 +286,7 @@ private void notifyChanged(Action action) {
}
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void addPlaybackControlsRow(AudioMetadataRetrievedEvent event) {
mPlaybackControlsRow = new PlaybackControlsRow(event);
mRowsAdapter.add(mPlaybackControlsRow);
Expand Down Expand Up @@ -358,6 +361,7 @@ private ImageView getBackground() {
return (ImageView) getActivity().findViewById(R.id.imageViewBackground);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Subscribe
public void onAudioMetadataRetrieved(AudioMetadataRetrievedEvent event) {
addPlaybackControlsRow(event);
Expand Down
Loading

0 comments on commit c94ae8b

Please sign in to comment.