Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cine.movies;
- import android.app.Activity;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.media.MediaPlayer.OnErrorListener;
- import android.os.Bundle;
- import android.view.View;
- import android.webkit.WebChromeClient;
- import android.webkit.WebView;
- import android.widget.FrameLayout;
- import android.widget.RelativeLayout;
- import android.widget.Toast;
- import android.widget.VideoView;
- public class ACT_ViewVideo extends Activity {
- private WebView webView;
- private VideoView mVideoView;
- private RelativeLayout mContentView;
- private FrameLayout mCustomViewContainer;
- private WebChromeClient.CustomViewCallback mCustomViewCallback;
- private String htmlCode = "<html><head><meta charset=\"utf-8\"></head><body style=\"background:#000000;margin:0;padding:0;\">"+
- "<video id=\"video\" poster=\"http://android.rpgsoluce.com/poster.png\" src=\"@VIDEO@\" width=\"100%\" height=\"100%\"></video>"+
- "<script>var video = document.getElementById('video'); video.addEventListener('click',function(){video.play();},false);</script>"+
- "</body></html>";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.layout_video);
- Intent intentReceived = this.getIntent();
- String trailer = intentReceived.getCharSequenceExtra("trailer").toString();
- try {
- webView = (WebView)findViewById(R.id.mainWebView);
- webView.getSettings().setJavaScriptEnabled(true);
- webView.getSettings().setAllowFileAccess(true);
- webView.getSettings().setPluginsEnabled(true);
- webView.setWebChromeClient(new MyChromeClient() {});
- webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); //thanks Patrick!
- htmlCode = htmlCode.replaceAll("@VIDEO@", trailer);
- webView.loadDataWithBaseURL("http://fake/fake", htmlCode, "text/html", "UTF-8", null);
- }
- catch (Exception e) {
- Toast.makeText(ACT_ViewVideo.this.getApplicationContext(), getString(R.string.error_problem), 30).show();
- }
- }
- private class MyChromeClient extends WebChromeClient implements OnCompletionListener, OnErrorListener{
- @Override
- public void onShowCustomView(View view, CustomViewCallback callback) {
- super.onShowCustomView(view, callback);
- if (view instanceof FrameLayout) {
- mCustomViewContainer = (FrameLayout) view;
- mCustomViewCallback = callback;
- mContentView = (RelativeLayout)findViewById(R.id.mainLayout); //layout that contains the webview
- if (mCustomViewContainer.getFocusedChild() instanceof VideoView) {
- mVideoView = (VideoView) mCustomViewContainer.getFocusedChild();
- mContentView.setVisibility(View.GONE);
- mCustomViewContainer.setVisibility(View.VISIBLE);
- setContentView(mCustomViewContainer);
- mVideoView.setOnCompletionListener(this);
- mVideoView.setOnErrorListener(this);
- mVideoView.start();
- }
- }
- }
- public void onHideCustomView() {
- if (mVideoView == null) {
- return;
- } else {
- // Hide the custom view.
- mVideoView.setVisibility(View.GONE);
- // Remove the custom view from its container.
- mCustomViewContainer.removeView(mVideoView);
- mVideoView = null;
- mCustomViewContainer.setVisibility(View.GONE);
- mCustomViewCallback.onCustomViewHidden();
- // Show the content view.
- mContentView.setVisibility(View.VISIBLE);
- }
- }
- public void onCompletion(MediaPlayer mp) {
- mp.stop();
- mCustomViewContainer.setVisibility(View.GONE);
- onHideCustomView();
- setContentView(mContentView);
- mCustomViewContainer = null;
- }
- public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
- setContentView(mContentView);
- return true;
- }
- }
- @Override
- public void onBackPressed() {
- if (mCustomViewContainer != null) {
- mVideoView.stopPlayback();
- mCustomViewContainer.setVisibility(View.GONE);
- if (mVideoView == null) {
- return;
- } else {
- // Hide the custom view.
- mVideoView.setVisibility(View.GONE);
- // Remove the custom view from its container.
- mCustomViewContainer.removeView(mVideoView);
- mVideoView = null;
- mCustomViewContainer.setVisibility(View.GONE);
- mCustomViewCallback.onCustomViewHidden();
- // Show the content view.
- mContentView.setVisibility(View.VISIBLE);
- setContentView(mContentView);
- mCustomViewContainer = null;
- }
- }
- else if (webView.canGoBack()) {
- webView.goBack();
- } else
- super.onBackPressed();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement