在Android中播放Facebook视频?

时间:2016-07-28 05:28:49

标签: android html5 facebook video

有没有办法在Android中使用自动播放播放Facebook视频?

我厌倦了使用WebView,但似乎html5 mobile不允许自动播放视频。

    WebSettings webSettings = mainWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setMediaPlaybackRequiresUserGesture(false);
    mainWebView.setWebChromeClient(new WebChromeClient());
    mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    mainWebView.loadUrl("https://www.facebook.com/video/embed?video_id=10154623311598812");

2 个答案:

答案 0 :(得分:1)

以下是Android中可用的视频播放器库:
https://github.com/brianwernick/ExoMedia

请参阅其演示代码,了解如何集成它。

请注意,它不会使用HTML5

定义一个布局,比如说activity_video_player:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:EMVideoView="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

<com.devbrackets.android.exomedia.EMVideoView
    android:id="@+id/video_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    EMVideoView:defaultControlsEnabled="true"/>
</FrameLayout>

Activity这样说VideoPlayerActivity

public class VideoPlayerActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener
{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_player);
    .
    .
    String videoUrl = "url";
    mVideoView.setOnPreparedListener(this);
    mVideoView.setVideoURI(Uri.parse(videoUrl));
    }

@Override
    public void onPrepared(MediaPlayer mp) {
        mVideoView.start();
    }

}

那就是它。让魔法发生。

答案 1 :(得分:0)

首先使用Webview获取VideoID和VideoData(src),然后将VideoData传递给VideoPlayer并播放。

  webView.setWebViewClient(new WebViewClient()
    {

        @Override
        public void onPageFinished(WebView view, String url)
        {

            //query_string = "" + url;
            //showtoast("URL loaded :"+url);
            // view.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()");
        }

        @Override
        public void onLoadResource(WebView view, String url)
        {
            //  query_string=""+url;

            webView.loadUrl("javascript:function PlayVideo(src,id)" +
                    "{"
                    +"FBDownloader.processVideo(src,id);"
                     +"var video=document.getElementById(id);"
                    +"video.play();"
                     +"}" +
                    "(function prepareVideo() " +
                    "{ "
                    + "var el = document.querySelectorAll('div[data-sigil]');"
                    +"var ID;"
                    +"var SRC;"
                    + "for(var i=0;i<el.length; i++)"
                    + "{"
                    + "var sigil = el[i].dataset.sigil;"
                    + "if(sigil.indexOf('inlineVideo') > -1)" +
                    "{"
                    + "delete el[i].dataset.sigil;"
                    + "console.log(i);"
                    + "var jsonData = JSON.parse(el[i].dataset.store);"
                    +"ID=jsonData['videoID'];"
                    +"SRC=jsonData['src'];"

                   // +"document.getElementById(\"'+jsonData['videoID']+'\")[0].play();"
                //   +"var video = document.getElementById(\"'+jsonData['videoID']+'\");"
                  //  +"video.play();"
                   // +"FBDownloader.processVideo(ID,SRC);"
                 + "el[i].setAttribute('onClick','PlayVideo(\\\"'+jsonData['src']+'\\\",\\\"'+jsonData['videoID']+'\\\");');"//'document.getElementsByTagName('video')[0].play(); ');"
              //    +"'FBDownloader.processVideo(\"'+jsonData['src']+'\",\"'+jsonData['videoID']+'\");');"
                    //   + "el[i].setAttribute('onClick', 'FBDownloader.processVideo(\"'+jsonData['src']+'\",\"'+jsonData['videoID']+'\");');"
                    + "}"
                    + "}"

                    + "})()");
            //*/

        }
    });


  @JavascriptInterface
   public void processVideo(final String vidData, final String vidID)
  {

    this.vidData=vidData;
    this.vidID=vidID;

     //PLAY VIDEO HERE
    videoView.play(vidId);

}