Last active
September 7, 2018 06:02
-
-
Save zeero0/50f0e630f0726481a3cfbfa3fa54c7b4 to your computer and use it in GitHub Desktop.
Play Youtube video in Full Screen and disable all interaction.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.com.my_app.activities; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.pm.ActivityInfo; | |
import android.media.MediaPlayer; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.Gravity; | |
import android.view.View; | |
import android.view.Window; | |
import android.view.WindowManager; | |
import android.widget.ImageButton; | |
import android.widget.MediaController; | |
import android.widget.RelativeLayout; | |
import android.widget.VideoView; | |
import app.com.my_app.R; | |
import app.com.my_app.utils.ColorOp; | |
public class VideoActivity extends AppCompatActivity { | |
Context context; | |
VideoView videoView; | |
Uri videoUri; | |
private static String VIDEO_URI = "VIDEO_URI"; | |
private RelativeLayout.LayoutParams layoutParams; | |
boolean fullScreen; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// remove title | |
requestWindowFeature(Window.FEATURE_NO_TITLE); | |
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, | |
WindowManager.LayoutParams.FLAG_FULLSCREEN); | |
setContentView(R.layout.activity_video); | |
context = this; | |
Intent intent = getIntent(); | |
if (intent.getExtras() != null){ | |
videoUri = Uri.parse(intent.getStringExtra(VIDEO_URI)); | |
} | |
initViews(); | |
} | |
private void initViews() { | |
videoView = (VideoView) findViewById(R.id.videoview); | |
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { | |
@Override | |
public void onCompletion(MediaPlayer mediaPlayer) { | |
videoView.postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
finish(); | |
} | |
}, 500); | |
} | |
}); | |
layoutParams = new RelativeLayout.LayoutParams( | |
RelativeLayout.LayoutParams.MATCH_PARENT, | |
RelativeLayout.LayoutParams.MATCH_PARENT | |
); | |
if (videoUri != null) { | |
videoView.setVideoURI(videoUri); | |
videoView.setMediaController(new MediaController(context){ | |
@Override | |
public void setAnchorView(View view) { | |
super.setAnchorView(view); | |
ImageButton btnFS = new ImageButton(context); | |
btnFS.setBackgroundColor(ColorOp.getColor(context, R.color.transparent)); | |
final int version = Build.VERSION.SDK_INT; | |
if (version >= 23) { | |
btnFS.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_fs)); | |
} else { | |
btnFS.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_fs)); | |
} | |
btnFS.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (fullScreen) { | |
fullScreen = false; | |
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | |
} else { | |
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); | |
fullScreen = true; | |
} | |
} | |
}); | |
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); | |
params.gravity = Gravity.RIGHT; | |
addView(btnFS, params); | |
} | |
}); | |
videoView.requestFocus(); | |
videoView.start(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.com.my_app.activities; | |
import android.app.ProgressDialog; | |
import android.content.Intent; | |
import android.graphics.Bitmap; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.WindowManager; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import app.com.my_app.R; | |
import app.com.my_app.utils.webviewvideo.VideoEnabledWebChromeClient; | |
import app.com.my_app.utils.webviewvideo.VideoEnabledWebView; | |
public class VideoActivity_Web extends AppCompatActivity { | |
private VideoEnabledWebView webView; | |
private VideoEnabledWebChromeClient webChromeClient; | |
Uri videoUri; | |
String videoURL; | |
String title; | |
private static String VIDEO_URI = "VIDEO_URI"; | |
private static String TITLE = "title"; | |
ProgressDialog prDialog; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_video__web); | |
Intent intent = getIntent(); | |
if (intent.getExtras() != null) { | |
videoUri = Uri.parse(intent.getStringExtra(VIDEO_URI)); | |
videoURL = intent.getStringExtra(VIDEO_URI); | |
title = intent.getStringExtra(TITLE); | |
} | |
// Save the web view | |
webView = (VideoEnabledWebView) findViewById(R.id.webView); | |
prDialog = new ProgressDialog(VideoActivity_Web.this); | |
prDialog.setMessage("Loading..."); | |
if (!title.isEmpty() && title != null) { | |
setTitle(title); | |
} | |
// Initialize the VideoEnabledWebChromeClient and set event handlers | |
View nonVideoLayout = findViewById(R.id.nonVideoLayout); // Your own view, read class comments | |
ViewGroup videoLayout = (ViewGroup) findViewById(R.id.videoLayout); // Your own view, read class comments | |
//noinspection all | |
View loadingView = getLayoutInflater().inflate(R.layout.view_loading_video, null); // Your own view, read class comments | |
webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors... | |
{ | |
// Subscribe to standard events, such as onProgressChanged()... | |
@Override | |
public void onProgressChanged(WebView view, int progress) { | |
// Your code... | |
} | |
}; | |
webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback() { | |
@Override | |
public void toggledFullscreen(boolean fullscreen) { | |
// Your code to handle the full-screen change, for example showing and hiding the title bar. Example: | |
if (fullscreen) { | |
WindowManager.LayoutParams attrs = getWindow().getAttributes(); | |
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; | |
attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; | |
getWindow().setAttributes(attrs); | |
if (android.os.Build.VERSION.SDK_INT >= 14) { | |
//noinspection all | |
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE); | |
} | |
} else { | |
WindowManager.LayoutParams attrs = getWindow().getAttributes(); | |
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; | |
attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; | |
getWindow().setAttributes(attrs); | |
if (android.os.Build.VERSION.SDK_INT >= 14) { | |
//noinspection all | |
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); | |
} | |
} | |
} | |
}); | |
webView.setWebChromeClient(webChromeClient); | |
// Call private class InsideWebViewClient | |
webView.setWebViewClient(new InsideWebViewClient()); | |
String url = null; | |
try { | |
url = videoURL.split("=")[1]; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
String html = "http://www.youtube.com/embed/" + url | |
+ "?&theme=dark&autohide=2&VQ=HD1080&rel=0&showinfo=0&iv_load_policy=3&autoplay=1&loop=1&fs=1"; | |
// Navigate anywhere you want, but consider that this classes have only been tested on YouTube's mobile site | |
webView.loadUrl(html); | |
webView.setLongClickable(true); | |
webView.setOnLongClickListener(new View.OnLongClickListener() { | |
@Override | |
public boolean onLongClick(View v) { | |
return true; | |
} | |
}); | |
} | |
private class InsideWebViewClient extends WebViewClient { | |
@Override | |
// Force links to be opened inside WebView and not in Default Browser | |
// Thanks http://stackoverflow.com/a/33681975/1815624 | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
if (url.contains("iv_load_policy=3&autoplay=1&loop=1&fs=1")) { | |
view.loadUrl(url); | |
} | |
return true; | |
} | |
@Override | |
public void onPageStarted(WebView view, String url, Bitmap favicon) { | |
prDialog.show(); | |
super.onPageStarted(view, url, favicon); | |
} | |
@Override | |
public void onPageFinished(WebView view, String url) { | |
prDialog.dismiss(); | |
super.onPageFinished(view, url); | |
} | |
@Override | |
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { | |
prDialog.dismiss(); | |
super.onReceivedError(view, errorCode, description, failingUrl); | |
} | |
} | |
@Override | |
public void onBackPressed() { | |
// Notify the VideoEnabledWebChromeClient, and handle it ourselves if it doesn't handle it | |
if (!webChromeClient.onBackPressed()) { | |
if (webView.canGoBack()) { | |
webView.goBack(); | |
} else { | |
// Standard back button implementation (for example this could close the app) | |
super.onBackPressed(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment