Javascript 使用其他应用程序从我的应用程序播放WebView视频';音乐
我使用下面的代码播放Youtube视频时,自动播放和静音功能正常,但当播放视频时,我的音乐应用程序停止工作。。 一次只有一个人在工作Javascript 使用其他应用程序从我的应用程序播放WebView视频';音乐,javascript,android,webview,youtube,android-music-player,Javascript,Android,Webview,Youtube,Android Music Player,我使用下面的代码播放Youtube视频时,自动播放和静音功能正常,但当播放视频时,我的音乐应用程序停止工作。。 一次只有一个人在工作 mWebView = (WebView) findViewById(R.id.web); String videoURL = "https://www.youtube.com/embed/R52bof3tvZs"; String vid = "<html><body style=\"margin: 0; padding
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");`
mWebView=(WebView)findViewById(R.id.web);
字符串videoURL=”https://www.youtube.com/embed/R52bof3tvZs";
字符串vid=“”;
WebChromeClient mWebChromeClient=新的WebChromeClient(){
public void onProgressChanged(WebView视图,int newProgress){
}
};
mWebView.getSettings().setPluginState(WebSettings.pluginInstate.ON);
setWebChromeClient(mWebChromeClient);
setWebViewClient(新的WebViewClient()){
公共void onPageFinished(WebView视图,字符串url){
加载URL(“javascript:(函数(){document.getElementsByTagName('video')[0].play();})()”;
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.setInitialScale(1);
mWebView.getSettings().SetLoadWithOverview模式(true);
mWebView.getSettings().setUseWideViewPort(true);
如果(Build.VERSION.SDK_INT<17){
Log.i(“GPSNETWORK”,“=17”);
mWebView.getSettings().SetMediaPlaybackRequireservesture(false);
}
String myUrl=“htmlbody style='margin:0px;padding:0px;”\n+
“script type='text/javascript'src='http://www.youtube.com/iframe_api“/scriptscript type='text/javascript'\n”+
“var播放器;\n”+
“函数onyoutubeiframeapiredy()\n”+
{player=new YT.player('playerId',{events:{onReady:onPlayerReady}}})}\n+
“函数onPlayerReady(事件){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&;branding=1'frameborder='0'\n”+
“/body/html”;
mWebView.loadData(“+Html.fromHtml(myUrl)、“text/Html”、“UTF-8”);`
请帮助我,提前谢谢。过了很长时间,当玩家状态准备好时,我用国旗来修正它, 在我执行暂停播放的地方放置(检查)那个标志,它可以让这两个同时运行
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 =
\“+bool(真)+”
}\n”
这里用的是这样的
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();*/
}
}
过了很长一段时间,当玩家状态准备好时,我用国旗来修正它, 在我执行暂停播放的地方放置(检查)那个标志,它可以让这两个同时运行
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 =
\“+bool(真)+”
;}\n“
这里用的是这样的
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();*/
}
}