gpt4 book ai didi

javascript - 将我的应用程序中的 WebView 视频与其他应用程序的音乐一起播放

转载 作者:行者123 更新时间:2023-11-28 05:34:42 25 4
gpt4 key购买 nike

我正在使用以下代码,它可以很好地播放具有自动播放和静音功能的 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 = "&lt;html&gt;&lt;body style='margin:0px;padding:0px;'&gt;\n" +
" &lt;script type='text/javascript' src='http://www.youtube.com/iframe_api'&gt;&lt;/script&gt;&lt;script type='text/javascript'&gt;\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" +
" &lt;/script&gt;\n" +
" &lt;iframe id='playerId' type='text/html' width='1280' height='720'\n" +
" src="+videoURL+"?enablejsapi=1&amp;rel=0&amp;playsinline=1&amp;autoplay=1&amp;showinfo=0&amp;autohide=1&amp;controls=0&amp;modestbranding=1' frameborder='0'&gt;\n" +
" &lt;/body&gt;&lt;/html&gt;";
mWebView.loadData(""+Html.fromHtml(myUrl), "text/html", "UTF-8");`

请帮助我,提前致谢。

最佳答案

过了很长一段时间,我修复了它,当玩家状态准备好时,标志变为 true,并将该标志放置(检查)我正在执行播放器暂停的位置,它可以同时运行两者

String myUrl = "&lt;html&gt;&lt;body style='margin:0px;padding:0px;'&gt;\n" +
" &lt;script type='text/javascript' src='http://www.youtube.com/iframe_api'&gt;&lt;/script&gt;&lt;script type='text/javascript'&gt;\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(true)+"\"

;}\n";

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();*/
}

}

关于javascript - 将我的应用程序中的 WebView 视频与其他应用程序的音乐一起播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39447727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com