我正在使用以下代码,它可以正常播放具有自动播放和静音功能的Youtube视频,但是当它播放视频时,我的音乐应用程序停止工作.. 一次只有一个工作......
mWebView = (WebView) findViewById(R.id.web);
String videoURL = "https://www.youtube.com/embed/R52bof3tvZs";
String vid = "<html><body style=\"margin: 0; padding: 0\"><iframe width=\"100%\" height=\"100%\" src=\""+videoURL+"\" type=\"text/html\" frameborder=\"0\"></iframe><body><html>";
WebChromeClient mWebChromeClient = new WebChromeClient(){
public void onProgressChanged(WebView view, int newProgress) {
}
};
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.setWebChromeClient(mWebChromeClient);
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
mWebView.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()");
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.setInitialScale(1);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
if (Build.VERSION.SDK_INT < 17) {
Log.i("GPSNETWORK", "<17");
} else {
Log.i("GPSNETWORK", Build.VERSION.SDK_INT+">=17");
mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
}
String myUrl = "<html><body style='margin:0px;padding:0px;'>\n" +
" <script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>\n" +
" var player;\n" +
" function onYouTubeIframeAPIReady()\n" +
" {player=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}\n" +
" function onPlayerReady(event){player.mute();player.setVolume(0);player.playVideo();}\n" +
" </script>\n" +
" <iframe id='playerId' type='text/html' width='1280' height='720'\n" +
" src="+videoURL+"?enablejsapi=1&rel=0&playsinline=1&autoplay=1&showinfo=0&autohide=1&controls=0&modestbranding=1' frameborder='0'>\n" +
" </body></html>";
mWebView.loadData(""+Html.fromHtml(myUrl), "text/html", "UTF-8");`
请提前帮助我。
答案 0 :(得分:0)
很长一段时间后,当玩家状态准备就绪时,我用旗帜修正了它, 并放置(检查)那个我执行暂停播放器的标志,它允许同时运行
String myUrl = "<html><body style='margin:0px;padding:0px;'>\n" +
" <script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>\n" +
" var player;\n" +
" function onYouTubeIframeAPIReady()\n" +
" {player=new YT.Player('playerId',{events:{onReady:onPlayerReady, onStateChange: onPlayerStateChange}})}\n" +
" var done = false;\n" +
" function onPlayerReady(event){player.mute();player.setVolume(0);player.playVideo();done =
\&#34;&#34 +布尔(真)+&#34; \&#34;
;} \ n&#34 ;;
private boolean bool(boolean b)
{
you_tube_started = b;
Log.e(TAG, "you_tube_started "+you_tube_started);
return b;
}
这里使用像这样
if (mMediaPlayer != null && isPrepared) {
if (mMediaPlayer.isPlaying()) {
Log.w(TAG, "AUDIOFOCUS_LOSS : Nothing you_tube_started "+you_tube_started);
if(!you_tube_started)
pausePlayer();
pausedDueToAudioFocusLoss = true;
/*mMediaPlayer.stop();
mMediaPlayer.reset();*/
}
}