Skip to content

Commit

Permalink
refactor playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
autoandshare committed May 31, 2021
1 parent ba6f122 commit f3a09ec
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 99 deletions.
69 changes: 35 additions & 34 deletions src/main/java/autoandshare/headvr/activity/VideoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import autoandshare.headvr.lib.Setting;
import autoandshare.headvr.lib.State;
import autoandshare.headvr.lib.VideoRenderer;
import autoandshare.headvr.lib.browse.IPlayList;
import autoandshare.headvr.lib.browse.PlayList;
import autoandshare.headvr.lib.controller.KeyControl;
import autoandshare.headvr.lib.controller.TouchControl;
Expand All @@ -44,6 +45,7 @@

public class VideoActivity extends GvrActivity implements
GvrView.StereoRenderer {
public static IPlayList playListS;

private static final String TAG = "VideoActivity";

Expand All @@ -53,7 +55,7 @@ public class VideoActivity extends GvrActivity implements

private BasicUI basicUI;
private Setting setting;
private PlayList playList;
private IPlayList playList;
private HeadControl headControl;
private KeyControl keyControl;
private VideoRenderer videoRenderer;
Expand Down Expand Up @@ -84,15 +86,21 @@ public void onCreate(Bundle savedInstanceState) {
cardboardView.setRenderer(this);
this.setGvrView(cardboardView);

// use static variable to pass parameter to new activiy
playList = playListS;
playListS = null;

Log.i("intent", "start");
Uri uri = this.getIntent().getData();
if (uri == null) {
if (uri == null && playList == null) {
finish();
return;
}

Log.i("intent", uri.toString());
playList = PlayList.getPlayList(uri, this);
if (playList == null) {
playList = new PlayList(uri);
}
playList.setActivity(this);
}


Expand Down Expand Up @@ -130,44 +138,37 @@ private void updateScreenVertical(int i) {
updateSettingWithId(Setting.id.VerticalDistance, i);
}

private Boolean playMediaFromList(int offset) {
lastEventTime = System.currentTimeMillis();
if (!playList.isReady()) {
state.message = "Loading play list";
} else {
loaded = true;

MediaWrapper mw = playList.next(offset);
private void playMediaFromList(int offset) {
if (videoRenderer == null) {
return;
}

state.currentIndex = playList.currentIndex();
state.count = playList.count();
lastEventTime = System.currentTimeMillis();

if (mw == null) {
state.errorMessage = "Invalid play list";
} else {
videoRenderer.playUri(mw);
state.message = "Loading play list";

new Thread(() -> {
synchronized (playList) {
MediaWrapper mw = playList.getMediaAtOffset(offset);
if (mw == null) {
state.errorMessage = "Invalid play list";
} else {
state.indexString = playList.getIndexString();
videoRenderer.playUri(mw);
}
}
}
return true;
}).start();
return;
}

private Boolean prevFile() {
return playMediaFromList(-1);
playMediaFromList(-1);
return true;
}

private Boolean nextFile() {
return playMediaFromList(1);
}


private boolean loaded = false;

private void loadFirstVideo() {
if (loaded) {
return;
}

playMediaFromList(0);
playMediaFromList(1);
return true;
}

private Boolean returnHome() {
Expand Down Expand Up @@ -232,7 +233,6 @@ public void onRendererShutdown() {

@Override
public void onNewFrame(HeadTransform headTransform) {
loadFirstVideo();

processHeadMotion(headTransform);

Expand Down Expand Up @@ -301,6 +301,7 @@ public void onSurfaceCreated(EGLConfig config) {
basicUI = new BasicUI(state);

videoRenderer = new VideoRenderer(this, state);
playMediaFromList(0); // play first video
}


Expand Down
3 changes: 3 additions & 0 deletions src/main/java/autoandshare/headvr/activity/VlcHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.net.URLDecoder;
import java.util.List;

import autoandshare.headvr.lib.browse.PlayList;

public class VlcHelper {
private static final String[] permissions = new String[]{
Manifest.permission.INTERNET,
Expand Down Expand Up @@ -88,6 +90,7 @@ public static void openList(String mrl, Context context, List<MediaWrapper> list
}

private static void startActivity(Context context, Uri uri) {
VideoActivity.playListS = new PlayList(uri);
Intent i = new Intent(context, VideoActivity.class);
i.setData(uri);
context.startActivity(i);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/autoandshare/headvr/lib/BasicUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private String getFileNameOrTitle() {
private void drawFileNameOrMessage(Canvas canvas) {
String fullTxt = (state.message != null) ?
state.message :
"" + state.currentIndex + "/" + state.count + " " + getFileNameOrTitle();
"" + state.indexString + " " + getFileNameOrTitle();

drawString(canvas,
TextUtils.ellipsize(fullTxt, leftAlignTextPaint, (endX - beginX) * 3 / 4, TextUtils.TruncateAt.END).toString(),
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/autoandshare/headvr/lib/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class State {
private static final String TAG = "VideoState";

// list info
public int count;
public int currentIndex;
public String indexString;

// player ready
public boolean videoLoaded;
Expand Down
41 changes: 29 additions & 12 deletions src/main/java/autoandshare/headvr/lib/VideoRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,14 @@ public VideoRenderer(VideoActivity activity, State state) {

videoScreen = new VRTexture2D();
videoScreen.getSurfaceTexture().setOnFrameAvailableListener((t) -> {
if (switchingVideo) {
return;
}
framesCount += 1;
if (framesCount == 1) {
onVideoLoaded();
synchronized (this) {
if (switchingVideo) {
return;
}
framesCount += 1;
if (framesCount == 1) {
onVideoLoaded();
}
}
});

Expand All @@ -279,6 +281,7 @@ public VideoRenderer(VideoActivity activity, State state) {

private void onVideoLoaded() {

Log.d(TAG, "onVideoLoaded");
IMedia m = mPlayer.getMedia();
state.title = m.getMeta(IMedia.Meta.Title);
m.release();
Expand Down Expand Up @@ -314,8 +317,12 @@ public void playUri(MediaWrapper mw) {
state.propertyKey = PathUtil.getKey(mw.getUri());

if (mPlayer != null) {
mPlayer.getVLCVout().detachViews();
mPlayer.release();
try {
mPlayer.setEventListener(null);
mPlayer.getVLCVout().detachViews();
mPlayer.release();
} catch (Exception e) {
}
}

if (fd != null) {
Expand Down Expand Up @@ -343,7 +350,7 @@ public void playUri(MediaWrapper mw) {
m.release();
}

private void resetAll() {
private synchronized void resetAll() {
switchingVideo = true;
framesCount = 0;
updatePositionRequested = false;
Expand Down Expand Up @@ -512,6 +519,7 @@ public void fillStateTime() {
public void pause() {
if (mPlayer != null) {
mPlayer.pause();
Log.d(TAG, "pause called");
}
state.playing = false;
}
Expand All @@ -527,8 +535,14 @@ private boolean ended() {
}

public void onEvent(MediaPlayer.Event event) {
Log.d(TAG, "got media player event 0x" + Integer.toHexString(event.type));
Log.d(TAG, "mplayer state " + mPlayer.getPlayerState());
Log.d(TAG, "got media player event 0x" + Integer.toHexString(event.type)
+ " mplayer state " + mPlayer.getPlayerState()
+ " state playing " + state.playing);

if (mPlayer.getPlayerState() == IMedia.State.Playing && state.playing == false) {
pause();
}

switch (event.type) {
case MediaPlayer.Event.EndReached:
state.playerState = "Ended";
Expand Down Expand Up @@ -618,8 +632,11 @@ public void onEvent(IMedia.Event event) {
}

private void playSubItem() {
IMedia m = mPlayer.getMedia();
if (!state.playing) {
return;
}

IMedia m = mPlayer.getMedia();
if (m != null) {

IMediaList subItems = m.subItems();
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/autoandshare/headvr/lib/browse/HeadVRChannel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package autoandshare.headvr.lib.browse;

import android.app.Activity;

import org.videolan.medialibrary.interfaces.media.MediaWrapper;

public class HeadVRChannel implements IPlayList {
@Override
public void setActivity(Activity activity) {

}

public String getIndexString() {
return null;
}

@Override
public MediaWrapper getMediaAtOffset(int offset) {
return null;
}
}
11 changes: 11 additions & 0 deletions src/main/java/autoandshare/headvr/lib/browse/IPlayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package autoandshare.headvr.lib.browse;

import android.app.Activity;

import org.videolan.medialibrary.interfaces.media.MediaWrapper;

public interface IPlayList {
public void setActivity(Activity activity);
public String getIndexString();
public MediaWrapper getMediaAtOffset(int offset);
}
Loading

0 comments on commit f3a09ec

Please sign in to comment.