Android WebView playing audio with javascript

前端 未结 2 1650
傲寒
傲寒 2020-12-17 04:42

I\'m trying to very quickly port over an html application with plays sound (with soundmanager2) to a native android app using WebView. From my research, I haven\'t seen any

2条回答
  •  遥遥无期
    2020-12-17 05:10

    Would this work for you?

        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.endsWith(".ogg")){
                    Log.d(TAG, "Reproducir archivo OGG");
                    Uri tempPath = Uri.parse(url);
                    MediaPlayer player = MediaPlayer.create(WebViewVideo.this, tempPath);
                    player.start();
                    return true;
                }else{
                    return super.shouldOverrideUrlLoading(view, url);
                }
            }
    
        });
    

    If you really want to use Javascript, you can try with document.open() or something like that, I haven't tried so, but I guess it would work the same way.

    By the way, this is the case that you want to play that audio on background, if you want to show the actual player, I think you have to implement the View by yourself.

    And if you were considering html5 audio tag, forget it. AFAIK Froyo doesn't support it in any way.

    I hope this helps and it's not too late.

    Regards.

提交回复
热议问题