问题
Is there any way to play facebook video with autoplay in Android?
I tired to use WebView, but it seems html5 mobile does not allow auto play video.
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");
回答1:
Here is a nice Video Player library available in Android:
https://github.com/brianwernick/ExoMedia
Please see its demo code to see how to integrate it.
Please not it does not uses HTML5
.
Define a layout say activity_video_player like this:
<?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>
And Activity
say VideoPlayerActivity
like this :
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();
}
}
That's it. Let the magic happen.
回答2:
First of all use Webview to get VideoID and VideoData (src) then pass VideoData to VideoPlayer and play it.
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);
}
来源:https://stackoverflow.com/questions/38627748/playing-facebook-video-in-android