Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
Added Cast
Browse files Browse the repository at this point in the history
  • Loading branch information
bostrot committed Feb 10, 2018
1 parent 1214056 commit 4be26ed
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 47 deletions.
15 changes: 8 additions & 7 deletions app/build.gradle
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "pro.bostrot.dtubeviewer"
minSdkVersion 21
targetSdkVersion 26
versionCode 6
versionName "1.6"
versionCode 7
versionName "1.7"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -31,11 +31,12 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile('com.crashlytics.sdk.android:crashlytics:2.8.0@aar') {
implementation('com.crashlytics.sdk.android:crashlytics:2.8.0@aar') {
transitive = true;
}
compile 'com.android.volley:volley:1.0.0'
compile 'com.google.android.exoplayer:exoplayer:2.6.1'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:mediarouter-v7:26.1.0'
implementation 'com.android.volley:volley:1.0.0'
implementation 'com.google.android.exoplayer:exoplayer:2.6.1'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:mediarouter-v7:26.1.0'
implementation 'com.google.android.gms:play-services-cast-framework:11.8.0'
}
17 changes: 17 additions & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -36,10 +36,27 @@
android:scheme="https" />
</intent-filter>
</activity>
<activity
android:name=".ExpandedControlsActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="pro.bostrot.dtubeviewer.MainActivity"/>
</activity>

<meta-data
android:name="io.fabric.ApiKey"
android:value="f4fd919b579adebf030f3671f8942bba1f7249c5" />
<meta-data
android:name=
"com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="pro.bostrot.dtubeviewer.CastOptionsProvider" />

</application>

Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/pro/bostrot/dtubeviewer/CastOptionsProvider.java
@@ -0,0 +1,36 @@
package pro.bostrot.dtubeviewer;

import android.content.Context;

import com.google.android.gms.cast.framework.CastOptions;
import com.google.android.gms.cast.framework.OptionsProvider;
import com.google.android.gms.cast.framework.SessionProvider;
import com.google.android.gms.cast.framework.media.CastMediaOptions;
import com.google.android.gms.cast.framework.media.NotificationOptions;

import java.util.List;

public class CastOptionsProvider implements OptionsProvider {

@Override
public CastOptions getCastOptions(Context context) {
NotificationOptions notificationOptions = new NotificationOptions.Builder()
.setTargetActivityClassName(ExpandedControlsActivity.class.getName())
.build();
CastMediaOptions mediaOptions = new CastMediaOptions.Builder()
.setNotificationOptions(notificationOptions)
.setExpandedControllerActivityClassName(ExpandedControlsActivity.class.getName())
.build();

return new CastOptions.Builder()
.setResumeSavedSession(false)
.setReceiverApplicationId(context.getString(R.string.app_id))
.setCastMediaOptions(mediaOptions)
.build();
}

@Override
public List<SessionProvider> getAdditionalSessionProviders(Context context) {
return null;
}
}
10 changes: 3 additions & 7 deletions app/src/main/java/pro/bostrot/dtubeviewer/CustomWebClient.java
Expand Up @@ -46,14 +46,10 @@ public void onSuccess(JSONObject string) {
try {
JSONObject video = new JSONObject(string.getJSONObject("result").getString("json_metadata"));
String sourceVideo = video.getJSONObject("video").getJSONObject("content").getString("videohash");
String worseVideo = video.getJSONObject("video").getJSONObject("content").getString("video480hash");
String compressedVideo = video.getJSONObject("video").getJSONObject("content").getString("video480hash");
VideoPlayer vp = new VideoPlayer();
Log.d("S/W", sourceVideo + ":" + worseVideo);
if (worseVideo != null && worseVideo.length() > 5) {
vp.video("https://gateway.ipfs.io/ipfs/" + worseVideo);
} else {
vp.video("https://gateway.ipfs.io/ipfs/" + sourceVideo);
}
Log.d("S/W", sourceVideo + ":" + compressedVideo);
vp.video(sourceVideo, compressedVideo);
} catch (JSONException e) {}
}
});
Expand Down
@@ -0,0 +1,17 @@
package pro.bostrot.dtubeviewer;

import android.view.Menu;

import com.google.android.gms.cast.framework.CastButtonFactory;
import com.google.android.gms.cast.framework.media.widget.ExpandedControllerActivity;

public class ExpandedControlsActivity extends ExpandedControllerActivity {

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.expanded_controller, menu);
CastButtonFactory.setUpMediaRouteButton(this, menu, R.id.media_route_menu_item);
return true;
}
}
159 changes: 157 additions & 2 deletions app/src/main/java/pro/bostrot/dtubeviewer/MainActivity.java
Expand Up @@ -13,12 +13,15 @@
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.MediaRouteButton;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -40,13 +43,26 @@
import java.util.List;

import com.crashlytics.android.Crashlytics;
import com.google.android.gms.cast.MediaInfo;
import com.google.android.gms.cast.MediaMetadata;
import com.google.android.gms.cast.framework.CastButtonFactory;
import com.google.android.gms.cast.framework.CastContext;
import com.google.android.gms.cast.framework.CastSession;
import com.google.android.gms.cast.framework.CastState;
import com.google.android.gms.cast.framework.CastStateListener;
import com.google.android.gms.cast.framework.Session;
import com.google.android.gms.cast.framework.SessionManager;
import com.google.android.gms.cast.framework.SessionManagerListener;
import com.google.android.gms.cast.framework.media.RemoteMediaClient;
import com.google.android.gms.common.images.WebImage;

import org.json.JSONException;
import org.json.JSONObject;

import io.fabric.sdk.android.Fabric;

import static pro.bostrot.dtubeviewer.VideoPlayer.isInFullscreen;
import static pro.bostrot.dtubeviewer.VideoPlayer.lastUrl;
import static pro.bostrot.dtubeviewer.VideoPlayer.player;

public class MainActivity extends AppCompatActivity {
Expand All @@ -69,8 +85,15 @@ public class MainActivity extends AppCompatActivity {
long position;
SteemitAPI steemitAPI;
VideoPlayer videoPlayer;
MediaRouteButton mMediaRouteButton;

MediaRouteButton mMediaRouteButton;
private CastSession mCastSession;
private SessionManager mSessionManager;
private final SessionManagerListener mSessionManagerListener =
new SessionManagerListenerImpl();
MediaMetadata movieMetadata;
MediaInfo mediaInfo;
CastContext mCastContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -79,6 +102,7 @@ protected void onCreate(Bundle savedInstanceState) {

activity = MainActivity.this;


// Keep alive
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
videoPlayer = new VideoPlayer();
Expand Down Expand Up @@ -184,9 +208,127 @@ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathC
} catch (Exception e) {
}

// Casting
mCastContext = CastContext.getSharedInstance(this);
mMediaRouteButton = (MediaRouteButton) findViewById(R.id.media_route_button);
CastButtonFactory.setUpMediaRouteButton(this, mMediaRouteButton);

mSessionManager = mCastContext.getSessionManager();
}

private void loadRemoteMedia(int position, boolean autoPlay) {
Log.d("CAST SESSION 2", lastUrl);
movieMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);

movieMetadata.putString(MediaMetadata.KEY_TITLE, "DTube");
movieMetadata.putString(MediaMetadata.KEY_SUBTITLE, "Bostrot is streaming content...");
movieMetadata.addImage(new WebImage(Uri.parse("https://steemit-production-imageproxy-upload.s3.amazonaws.com/DQmeddeVdpUn2d6VKNTNt9EGDYRucxKPAZRKodA88k2YtB9")));
movieMetadata.addImage(new WebImage(Uri.parse("https://steemit-production-imageproxy-upload.s3.amazonaws.com/DQmeddeVdpUn2d6VKNTNt9EGDYRucxKPAZRKodA88k2YtB9")));

mediaInfo = new MediaInfo.Builder(lastUrl)
.setStreamType(MediaInfo.STREAM_TYPE_LIVE)
.setContentType("videos/mp4")
.setMetadata(movieMetadata)
.build();

if (mCastSession == null) {
Log.d("mCastSession", "null");
return;
}
final RemoteMediaClient remoteMediaClient = mCastSession.getRemoteMediaClient();
if (remoteMediaClient == null) {
Log.d("remoteMediaClient", "null");
return;
}
remoteMediaClient.addListener(new RemoteMediaClient.Listener() {

@Override
public void onStatusUpdated() {
Intent intent = new Intent(MainActivity.this, ExpandedControlsActivity.class);
startActivity(intent);
remoteMediaClient.removeListener(this);
}

@Override
public void onMetadataUpdated() {
}

@Override
public void onQueueStatusUpdated() {
}

@Override
public void onPreloadStatusUpdated() {
}

@Override
public void onAdBreakStatusUpdated() {
}
@Override
public void onSendingRemoteMediaRequest() {
}
});
remoteMediaClient.load(mediaInfo, autoPlay, position);
}


@Override public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.browse, menu);
CastButtonFactory.setUpMediaRouteButton(getApplicationContext(),
menu,
R.id.media_route_menu_item);
return true;
}

private class SessionManagerListenerImpl implements SessionManagerListener<CastSession> {
@Override
public void onSessionStarted(CastSession session, String sessionId) {
mCastSession = session;
Log.d("CAST SESSION", "started");
player.release();
loadRemoteMedia(0, true);
invalidateOptionsMenu();
}
@Override
public void onSessionResumed(CastSession session, boolean wasSuspended) {
mCastSession = session;
invalidateOptionsMenu();
}
@Override
public void onSessionEnded(CastSession session, int error) {
Log.d("Session", "Ended");
if (session == mCastSession) {
mCastSession = null;
}
}
@Override
public void onSessionEnding(CastSession session) {
Log.d("Session", "Ended2");
}
@Override
public void onSessionResumeFailed(CastSession session, int error) {
Log.d("Session", "Ended3");
}
@Override
public void onSessionStartFailed(CastSession session, int error) {
Log.d("Session", "Ended4");
}
@Override
public void onSessionResuming(CastSession session, String sessionId) {
Log.d("Session", "Ended5");
}
@Override
public void onSessionStarting(CastSession session) {
Log.d("Session", "Ended6");
}
@Override
public void onSessionSuspended(CastSession session, int error) {
Log.d("Session", "Ended7");
}
}


public void webViewSettings(WebSettings webSettings) {
webSettings.setJavaScriptEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
Expand Down Expand Up @@ -224,6 +366,7 @@ public void onPictureInPictureModeChanged (boolean isInPictureInPictureMode, Con

@Override
public void onPause() {

if (Build.VERSION.SDK_INT > 24) {
Log.d("PIP", "here!");
if (isInPictureInPictureMode()) {
Expand All @@ -235,6 +378,16 @@ public void onPause() {
}
}
super.onPause();
mSessionManager.removeSessionManagerListener(mSessionManagerListener);
mCastSession = null;
}


@Override
protected void onResume() {
mCastSession = mSessionManager.getCurrentCastSession();
mSessionManager.addSessionManagerListener(mSessionManagerListener);
super.onResume();
}

// Create an image file
Expand All @@ -253,7 +406,9 @@ public void onBackPressed() {
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
findViewById(R.id.videoPlayer).setVisibility(View.GONE);
player.release();
if (player.getCurrentTimeline() != null) {
player.release();
}
wv.goBack();
}
} else {
Expand Down
Expand Up @@ -155,29 +155,6 @@ public void onShowCustomView(View view, CustomViewCallback callback)
{
//wv.loadUrl("javascript:alert($('iframe').contents().find('.vjs-current-time-display').text().split('Current Time ')[1].split(':')[0] * 60 + parseInt($('iframe').contents().find('.vjs-current-time-display').text().split('Current Time ')[1].split(':')[1]))");

String tempURL = webView.getUrl();
tempURL = tempURL.split("#!/v/")[1];
String account = tempURL.split("/")[0];
String permalink = tempURL.split("/")[1];
// Hide Status Bar
MainActivity.activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

// TESTING
SteemitAPI steemitAPI = new SteemitAPI();
steemitAPI.getContent(account, permalink, new SteemitAPI.VolleyCallback() {
@Override
public void onSuccess(JSONObject string) {
try {
JSONObject video = new JSONObject(string.getJSONObject("result").getString("json_metadata"));
Log.d("SESSION22", video.getJSONObject("video").getJSONObject("content").getString("videohash"));
Log.d("SESSION22", video.getJSONObject("video").getJSONObject("content").getString("video480hash"));
String url = "https://scrappy.i.ipfs.io/ipfs/" + video.getJSONObject("video").getJSONObject("content").getString("video480hash");
VideoPlayer vp = new VideoPlayer();
vp.video(url);
} catch (JSONException e) {}
}
});

if (view instanceof FrameLayout)
{
// A video wants to be shown
Expand Down

0 comments on commit 4be26ed

Please sign in to comment.