【问题标题】:Unable to play html5 streaming video in android web view无法在 android web 视图中播放 html5 流媒体视频
【发布时间】:2011-11-22 07:56:56
【问题描述】:

我购买了 Foscam 安全摄像头,并且能够在我的 MacBook 上看到 JPEG 流。 但是,当我使用 chrome 在手机浏览器中打开相同的链接时,它会开始下载一些不确定的内容,并且在通知菜单中显示下载不成功。

另外,如果我在我的 Android Firefox 浏览器上打开相同的链接,我就能看到视频。

我必须创建一个 android 应用程序来显示视频流,就像在笔记本电脑浏览器上一样。

以下是我正在使用的代码:

package org.securitycamera;


import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.VideoView;

public class SecuritycameraActivity extends Activity {
    WebView webView;
    FrameLayout frameLayout;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater inflator = getLayoutInflater();
        View inflatedView = inflator.inflate(R.layout.main, null); 

        if (!(inflatedView instanceof FrameLayout))
        {
            throw new RuntimeException("inflated view not FrameLayout");
        }
        else
        {
            frameLayout = (FrameLayout)inflatedView;
        }

        setContentView(frameLayout);

        webView = (WebView) findViewById(R.id.wv);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setPluginState(android.webkit.WebSettings.PluginState.ON);
        webView.setWebChromeClient(new MyWebChromeClient());         


        try
        {
           // webView.loadUrl("http://192.168.1.6/videostream.cgi?user=admin&pwd=");
            webView.loadUrl("http://broken-links.com/tests/video/");
        }
        catch(Exception e)
        {
            throw new RuntimeException();
        }

    }

    private class MyWebChromeClient extends WebChromeClient implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener {
        VideoView videoView;
        WebChromeClient.CustomViewCallback customViewCallback;

        public void onProgressChanged(WebView view, int newProgress)
        {
            if (newProgress == 100) 
            { 
                view.loadUrl("javascript:playVideo()");
            }

        }

        public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
        {
             if (view instanceof FrameLayout){
                 FrameLayout frame = (FrameLayout) view;
                 if (frame.getFocusedChild() instanceof VideoView){
                     VideoView video = (VideoView) frame.getFocusedChild();
                     frame.removeView(video);
                     setContentView(video);
                     video.setOnCompletionListener(new OnCompletionListener() {

                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            //Log.i(DVNGActivity.TAG, "LoadData_QRURL --> onCompletion...");
                            mp.stop();
                            setContentView(R.layout.main);
                        }
                    });
                     video.setOnErrorListener(new OnErrorListener() {

                        @Override
                        public boolean onError(MediaPlayer mp, int what, int extra) {
                        //  Log.i(DVNGActivity.TAG, "LoadData_QRURL --> onError");
                            return false;
                        }
                    });
                     video.start();
                 }
             }

        }

        public void onPrepared(MediaPlayer mp)
        {
        }

        public void onCompletion(MediaPlayer mp)
        {
          // this is needed to release the MediaPlayer and its resources so it can
          // be used again later 
          videoView.stopPlayback();

          // now remove the video and tell the callback to hide the custom view 
          frameLayout.removeView(videoView);
          customViewCallback.onCustomViewHidden();

          finish();
        }

        public boolean onError(MediaPlayer mp, int what, int extra)
        {
            return false; // we did not handle the error - onCompletion will be called
        }
    }
}

我关注了这个How to Play HTML5 video and YouTube Video within Android WebView?,如果不是在示例中播放视频,而是播放我的安全摄像头的视频,即

webView.loadUrl("http://192.168.1.6/videostream.cgi?user=admin&pwd=");

我得到一个白屏。

【问题讨论】:

    标签: android html uiwebview


    【解决方案1】:

    这个示例https://gist.github.com/3718414 有一个 Android webview 包装器和 HTML5 视频——它不像仅仅引用 URL 那样简单(如果你想要 webView 中的视频)但在 ICS 及更高版本上并不难(确保你有硬件加速启用似乎很关键)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多