【问题标题】:How can i play videos in webview android?如何在 webview android 中播放视频?
【发布时间】:2013-02-08 06:44:56
【问题描述】:

由于我对 Java 和 Android 应用程序很陌生,我不得不问这个问题,因为我工作了 10 个小时,却什么也做不了。我在 Eclipse 中制作了一个全屏网络应用程序,但我无法在其中看到视频。这是我的代码:

Tarim.java

package com.tarim.tarimvideo;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import com.tarim.tarimvideo.util.SystemUiHider;

@SuppressLint("SetJavaScriptEnabled")
public class Tarim extends Activity {

private static final boolean AUTO_HIDE = true;
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
private static final boolean TOGGLE_ON_CLICK = true;
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
private SystemUiHider mSystemUiHider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_tarim);

    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content);
    WebView webView = (WebView)findViewById(R.id.tarayici);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.youtube.com/watch?v=W8SCqLkHDqw");


    // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mSystemUiHider = SystemUiHider.getInstance(this, contentView,
            HIDER_FLAGS);
    mSystemUiHider.setup();
    mSystemUiHider
            .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
                // Cached values.
                int mControlsHeight;
                int mShortAnimTime;

                @Override
                @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
                public void onVisibilityChange(boolean visible) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                        // If the ViewPropertyAnimator API is available
                        // (Honeycomb MR2 and later), use it to animate the
                        // in-layout UI controls at the bottom of the
                        // screen.
                        if (mControlsHeight == 0) {
                            mControlsHeight = controlsView.getHeight();
                        }
                        if (mShortAnimTime == 0) {
                            mShortAnimTime = getResources().getInteger(
                                    android.R.integer.config_shortAnimTime);
                        }
                        controlsView
                                .animate()
                                .translationY(visible ? 0 : mControlsHeight)
                                .setDuration(mShortAnimTime);
                    } else {
                        // If the ViewPropertyAnimator APIs aren't
                        // available, simply show or hide the in-layout UI
                        // controls.
                        controlsView.setVisibility(visible ? View.VISIBLE
                                : View.GONE);
                    }

                    if (visible && AUTO_HIDE) {
                        // Schedule a hide().
                        delayedHide(AUTO_HIDE_DELAY_MILLIS);
                    }
                }
            });

    // Set up the user interaction to manually show or hide the system UI.
    contentView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (TOGGLE_ON_CLICK) {
                mSystemUiHider.toggle();
            } else {
                mSystemUiHider.show();
            }
        }
    }); 

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    // Trigger the initial hide() shortly after the activity has been
    // created, to briefly hint to the user that UI controls
    // are available.
    delayedHide(100);
}
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (AUTO_HIDE) {
            delayedHide(AUTO_HIDE_DELAY_MILLIS);
        }
        return false;
    }
};
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
    @Override
    public void run() {
        mSystemUiHider.hide();
    }
};
private void delayedHide(int delayMillis) {
    mHideHandler.removeCallbacks(mHideRunnable);
    mHideHandler.postDelayed(mHideRunnable, delayMillis);
}   
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tarim.tarimvideo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name="com.tarim.tarimvideo.Tarim"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

activity.tarim.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context=".Tarim" >

<!--
     The primary full-screen view. This can be replaced with whatever view
     is needed to present your content, e.g. VideoView, SurfaceView,
     TextureView, etc.
-->

<TextView
    android:id="@+id/fullscreen_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:keepScreenOn="true"
    android:text="@string/dummy_content"
    android:textColor="#33b5e5"
    android:textSize="50sp"
    android:textStyle="bold" />

<!--
     This FrameLayout insets its children based on system windows using
     android:fitsSystemWindows.
-->

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <LinearLayout
        android:id="@+id/fullscreen_content_controls"
        style="?buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:background="@color/black_overlay"
        android:orientation="horizontal"
        tools:ignore="UselessParent" >

        <Button
            android:id="@+id/dummy_button"
            style="?buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/dummy_button" />
    </LinearLayout>

    <WebView
        android:id="@+id/tarayici"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

</FrameLayout>

请注意,我在这个 Java 方面只学习了 10 个小时,所以如果你能尽可能简单地解释我,那就太好了。

【问题讨论】:

    标签: android html video webview


    【解决方案1】:

    如果没有 Flash 的帮助,在 WebView 中似乎是不可能的。请看这个:Youtube in a webview(供参考)。并查看此 Flash 演示:Flash demo。最后this 可能也有帮助,它是 Android 的 YouTube API。

    【讨论】:

    • 感谢 GLaDOS 的快速回复,但正如我在帖子中所说,我对开发一些东西很陌生。我只是想了解将此代码放在我的 .java 文件中的什么位置。
    • '感谢 GLaDOS 的快速回复,但正如我在帖子中所说,我对开发一些东西很陌生。你能告诉我把这段代码放在我的 .java 文件中的什么位置吗?
    • @KayahanKahriman 第二个链接中的代码将进入您用于显示视频的 Activity(可能在 onCreate 方法中)。
    【解决方案2】:

    只需将包含视频链接的 .html 文件放入 SDCARD 中。(将视频链接称为 content:\\)。然后将该 .html 文件加载到 webview。

    【讨论】:

    • a4android 能不能再具体一点?
    【解决方案3】:

    首先关心编码。这是article with a working example 和一些为 Android webkit 编码视频的指南。

    然后...当我不得不面对这个问题时,我不得不进行一些研究并找到一些有用的答案。基本上你必须以原生浏览器的方式打开视频

    public class InredisChromeClient extends WebChromeClient implements OnCompletionListener, OnErrorListener {
        private InterfazWebInredis interfazWeb; // Use Your WebView instance instead
    
        private VideoView mCustomVideoView;
    
        private LinearLayout mContentView;
        private FrameLayout mCustomViewContainer;
        private WebChromeClient.CustomViewCallback mCustomViewCallback;
        private LinearLayout mErrorConsoleContainer;
        static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT, Gravity.CENTER);
    
        public InredisChromeClient(InterfazWebInredis iwi) {
            super();
            this.interfazWeb = iwi;
        }
    
        public void onShowCustomView(View view, CustomViewCallback callback) {
            // super.onShowCustomView(view, callback);
            if (view instanceof FrameLayout) {
                mCustomViewContainer = (FrameLayout) view;
                mCustomViewCallback = callback;
                mContentView = (LinearLayout) interfazWeb.findViewById(R.id.mainContainer);
                if (mCustomViewContainer.getFocusedChild() instanceof VideoView) {
                    mCustomVideoView = (VideoView) mCustomViewContainer.getFocusedChild();
                    // frame.removeView(video);
                    mContentView.setVisibility(View.GONE);
                    mCustomViewContainer.setVisibility(View.VISIBLE);
                    interfazWeb.setContentView(mCustomViewContainer);
                    mCustomVideoView.setOnCompletionListener(this);
                    mCustomVideoView.setOnErrorListener(this);
                    mCustomVideoView.start();
                }
            }
        }
    
        public void onHideCustomView() {
            if (mCustomVideoView == null)
                return;
            // Hide the custom view.
            mCustomVideoView.setVisibility(View.GONE);
            // Remove the custom view from its container.
            mCustomViewContainer.removeView(mCustomVideoView);
            mCustomVideoView = null;
            mCustomViewContainer.setVisibility(View.GONE);
            mCustomViewCallback.onCustomViewHidden();
            // Show the content view.
            mContentView.setVisibility(View.VISIBLE);
        }
    
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.stop();
            mCustomViewContainer.setVisibility(View.GONE);
            onHideCustomView();
            interfazWeb.setContentView(mContentView);
        }
    
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            interfazWeb.setContentView(R.layout.main);
            return true;
        }
    }
    

    所以,这段代码的灵感来自android project source code of the browser

    好吧,这个行为是全屏打开视频。我不知道是否可以在网页内的自己的框架中播放视频。但是这个解决方案对我有用,我也希望对你有用。

    问候

    【讨论】:

    • 你好Android用户,你能解释一下如何使用这个文件吗?因为我以前读过这篇文章,但无法使用我的代码。
    猜你喜欢
    • 2011-12-31
    • 1970-01-01
    • 2013-12-17
    • 2013-07-20
    • 1970-01-01
    • 2012-12-16
    • 2011-10-22
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多