使用其他应用程序的音乐从我的应用程序播放WebView视频

时间:2016-09-12 09:56:33

标签: 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: 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");` 

请提前帮助我。

1 个答案:

答案 0 :(得分:0)

很长一段时间后,当玩家状态准备就绪时,我用旗帜修正了它, 并放置(检查)那个我执行暂停播放器的标志,它允许同时运行

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 = 
  

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

                }