Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video player updates #216

Merged
merged 4 commits into from
Jun 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private void showAudio() {

private void showAudioControls() {
if (areAudioControlsAvailable() && !audioControls.isShowing()) {
audioControls.showAnimated();
audioControls.show(0);
}
}

Expand Down Expand Up @@ -339,7 +339,7 @@ private void hideAudioMetadata() {

private void hideAudioControls() {
if (areAudioControlsAvailable() && audioControls.isShowing()) {
audioControls.hideAnimated();
audioControls.hide();
}
}

Expand Down Expand Up @@ -423,7 +423,7 @@ protected void onResume() {

private void showAudioControlsForced() {
if (areAudioControlsAvailable() && !audioControls.isShowing()) {
audioControls.show();
audioControls.show(0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

package org.amahi.anywhere.activity;

import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
Expand All @@ -35,8 +37,11 @@
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.Toast;

import org.amahi.anywhere.AmahiApplication;
import org.amahi.anywhere.R;
Expand All @@ -45,7 +50,6 @@
import org.amahi.anywhere.service.VideoService;
import org.amahi.anywhere.util.FullScreenHelper;
import org.amahi.anywhere.util.Intents;
import org.amahi.anywhere.util.ViewDirector;
import org.amahi.anywhere.view.MediaControls;
import org.videolan.libvlc.IVLCVout;
import org.videolan.libvlc.Media;
Expand All @@ -63,6 +67,8 @@ public class ServerFileVideoActivity extends AppCompatActivity implements
MediaPlayer.EventListener,
View.OnLayoutChangeListener {

private static final boolean ENABLE_SUBTITLES = true;

private VideoService videoService;
private MediaControls videoControls;
private FullScreenHelper fullScreen;
Expand All @@ -75,6 +81,9 @@ public class ServerFileVideoActivity extends AppCompatActivity implements
private int mVideoSarNum = 0;
private int mVideoSarDen = 0;

private SurfaceView mSubtitlesSurface = null;
private float bufferPercent = 0.0f;

private enum SurfaceSizes {
SURFACE_BEST_FIT,
SURFACE_FIT_SCREEN,
Expand All @@ -85,7 +94,7 @@ private enum SurfaceSizes {
}

private static SurfaceSizes CURRENT_SIZE = SurfaceSizes.SURFACE_BEST_FIT;

//TODO Add feature for changing the screen size

@Override
Expand All @@ -97,6 +106,8 @@ protected void onCreate(Bundle savedInstanceState) {

setUpHomeNavigation();

setUpViews();

setUpVideo();

setUpFullScreen();
Expand All @@ -113,6 +124,15 @@ private void setUpHomeNavigation() {
getSupportActionBar().setIcon(R.drawable.ic_launcher);
}

private void setUpViews() {
if (ENABLE_SUBTITLES) {
final ViewStub stub = (ViewStub) findViewById(R.id.subtitles_stub);
mSubtitlesSurface = (SurfaceView) stub.inflate();
mSubtitlesSurface.setZOrderMediaOverlay(true);
mSubtitlesSurface.getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
}

private void setUpVideo() {
setUpVideoTitle();
}
Expand All @@ -126,9 +146,9 @@ private ServerFile getVideoFile() {
}

private void setUpFullScreen() {
fullScreen = new FullScreenHelper(getSupportActionBar(), getSurfaceFrame(), getControlsContainer());
fullScreen = new FullScreenHelper(getSupportActionBar(), getVideoMainFrame());
fullScreen.enableOnClickToggle(false);
getSurfaceFrame().setOnClickListener(new View.OnClickListener() {
getVideoMainFrame().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fullScreen.toggle();
Expand Down Expand Up @@ -185,6 +205,8 @@ private void setUpVideoView() {
surfaceHolder.setKeepScreenOn(true);
final IVLCVout vlcVout = getMediaPlayer().getVLCVout();
vlcVout.setVideoView(getSurface());
if (mSubtitlesSurface != null)
vlcVout.setSubtitlesView(mSubtitlesSurface);
vlcVout.attachViews(this);
getMediaPlayer().setEventListener(this);
}
Expand All @@ -194,13 +216,16 @@ private SurfaceView getSurface() {
}

private FrameLayout getSurfaceFrame() {
return (FrameLayout) findViewById(R.id.layout_content);
return (FrameLayout) findViewById(R.id.video_surface_frame);
}

private FrameLayout getVideoMainFrame() {
return (FrameLayout) findViewById(R.id.video_main_frame);
}

private void setUpVideoControls() {
if (!areVideoControlsAvailable()) {
videoControls = new MediaControls(this);

videoControls.setMediaPlayer(this);
videoControls.setAnchorView(getControlsContainer());
}
Expand All @@ -226,7 +251,6 @@ private View getControlsContainer() {

private void setUpVideoPlayback() {
if (videoService.isVideoStarted()) {
showVideo();
showThenAutoHideControls();
} else {
videoService.startVideo(getVideoShare(), getVideoFile());
Expand All @@ -238,13 +262,12 @@ private void showThenAutoHideControls() {
if (!isFinishing()) {
fullScreen.show();
fullScreen.delayedHide();
videoControls.showAnimated();
videoControls.hideControlsDelayed();
videoControls.show();
}
}

private void showVideo() {
ViewDirector.of(this, R.id.animator).show(R.id.layout_content);
private ProgressBar getProgressBar() {
return (ProgressBar) findViewById(android.R.id.progress);
}

private ServerShare getVideoShare() {
Expand All @@ -271,6 +294,7 @@ public void run() {
}
};

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onNewVideoLayout(IVLCVout vout, int width, int height, int visibleWidth, int visibleHeight, int sarNum, int sarDen) {
mVideoWidth = width;
Expand Down Expand Up @@ -372,12 +396,17 @@ private void updateVideoSurfaces() {
lp.width = (int) Math.ceil(dw * mVideoWidth / mVideoVisibleWidth);
lp.height = (int) Math.ceil(dh * mVideoHeight / mVideoVisibleHeight);
getSurface().setLayoutParams(lp);
if (mSubtitlesSurface != null)
mSubtitlesSurface.setLayoutParams(lp);

// set frame size (crop if necessary)
lp = getSurfaceFrame().getLayoutParams();
lp.width = (int) Math.floor(dw);
lp.height = (int) Math.floor(dh);
getSurfaceFrame().setLayoutParams(lp);
getSurface().invalidate();
if (mSubtitlesSurface != null)
mSubtitlesSurface.invalidate();
}

private void changeMediaPlayerLayout(int displayW, int displayH) {
Expand Down Expand Up @@ -482,7 +511,7 @@ public boolean isPlaying() {

@Override
public int getBufferPercentage() {
return 0;
return (int) bufferPercent;
}

@Override
Expand All @@ -495,9 +524,10 @@ public void onEvent(MediaPlayer.Event event) {

switch(event.type) {
case MediaPlayer.Event.MediaChanged:
showVideo();
getVideoMainFrame().setVisibility(View.VISIBLE);
break;
case MediaPlayer.Event.Playing:
getProgressBar().setVisibility(View.INVISIBLE);
showThenAutoHideControls();
break;
case MediaPlayer.Event.Paused:
Expand All @@ -507,11 +537,10 @@ public void onEvent(MediaPlayer.Event event) {
finish();
break;
case MediaPlayer.Event.Buffering:
// Log.d("BUFFERING", ""+event.getBuffering());
// TODO Use this and show buffering to users
bufferPercent = event.getBuffering();
break;
case MediaPlayer.Event.EncounteredError:
// TODO Handle errors encountered if any
Toast.makeText(this, R.string.message_error_video, Toast.LENGTH_SHORT).show();
break;
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/amahi/anywhere/server/model/ServerFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public String getName() {
return name;
}

public String getNameOnly() {
return name.replace("." + getExtension(), "");
}

public String getMime() {
return mime;
}
Expand Down Expand Up @@ -102,6 +106,15 @@ public String getPath() {
return uri.build().getPath();
}

public String getExtension() {
String[] splitString = name.split("\\.");
if(splitString.length > 1) {
return splitString[splitString.length-1];
} else {
return "";
}
}

public static final Creator<ServerFile> CREATOR = new Creator<ServerFile>()
{
@Override
Expand Down
50 changes: 49 additions & 1 deletion src/main/java/org/amahi/anywhere/service/VideoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@
import android.os.Binder;
import android.os.IBinder;

import com.squareup.otto.Subscribe;

import org.amahi.anywhere.AmahiApplication;
import org.amahi.anywhere.bus.BusProvider;
import org.amahi.anywhere.bus.ServerFilesLoadedEvent;
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.util.Mimes;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

Expand Down Expand Up @@ -63,6 +69,8 @@ public void onCreate() {
setUpInjections();

setUpVideoPlayer();

BusProvider.getBus().register(this);
}

private void setUpInjections() {
Expand Down Expand Up @@ -90,6 +98,7 @@ public void startVideo(ServerShare videoShare, ServerFile videoFile) {
private void setUpVideoPlayback() {
Media media = new Media(mLibVLC, getVideoUri());
mMediaPlayer.setMedia(media);
searchSubtitleFile();
media.release();
mMediaPlayer.play();
}
Expand All @@ -98,6 +107,45 @@ private Uri getVideoUri() {
return serverClient.getFileUri(videoShare, videoFile);
}

private void searchSubtitleFile() {
if (serverClient.isConnected()){
if (!isDirectoryAvailable()) {
serverClient.getFiles(videoShare);
} else {
serverClient.getFiles(videoShare, getDirectory());
}
}
}

@Subscribe
public void onFilesLoaded(ServerFilesLoadedEvent event) {
List<ServerFile> files = event.getServerFiles();
for (ServerFile file:files) {
if (videoFile.getNameOnly().equals(file.getNameOnly())) {
if (Mimes.match(file.getMime()) == Mimes.Type.SUBTITLE
|| file.getExtension().equals("srt")
|| file.getExtension().equals("sub")) {
mMediaPlayer.getMedia().addSlave(
new Media.Slave(
Media.Slave.Type.Subtitle, 4, getSubtitleUri(file)));
break;
}
}
}
}

private String getSubtitleUri(ServerFile file) {
return serverClient.getFileUri(videoShare, file).toString();
}

private boolean isDirectoryAvailable() {
return getDirectory() != null;
}

private ServerFile getDirectory() {
return videoFile.getParentFile();
}

public MediaPlayer getMediaPlayer() {
return mMediaPlayer;
}
Expand All @@ -114,12 +162,12 @@ public void pauseVideo() {
mMediaPlayer.pause();
}


@Override
public void onDestroy() {
super.onDestroy();

tearDownVideoPlayback();
BusProvider.getBus().unregister(this);
}


Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/amahi/anywhere/util/Mimes.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public class Mimes {
types.put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", Type.SPREADSHEET);

types.put("application/x-quicktimeplayer", Type.VIDEO);

types.put("application/x-subrip", Mimes.Type.SUBTITLE);
types.put("image/vnd.dvb.subtitle", Mimes.Type.SUBTITLE);
}

public static int match(String mime) {
Expand Down Expand Up @@ -127,6 +130,7 @@ public static final class Type {
public static final int PRESENTATION = 7;
public static final int SPREADSHEET = 8;
public static final int VIDEO = 9;
public static final int SUBTITLE = 10;
private Type() {
}

Expand Down
Loading