无法在android web视图中播放html5流媒体视频

时间:2011-11-22 07:56:57

标签: android html5 uiwebview

我已经购买了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=");

我得到一个白色的屏幕。

1 个答案:

答案 0 :(得分:0)

此示例https://gist.github.com/3718414有一个Android webview包装器和HTML5视频 - 它并不像引用URL那么简单(如果你想在webView中看到视频),但在ICS及以上版本上并不困难(确保你有启用硬件加速似乎非常关键)