【问题标题】:How to play audio on webview in android?如何在android的webview上播放音频?
【发布时间】:2016-04-26 06:42:09
【问题描述】:

我想在 webview 上播放来自 URL 的音频。我也希望它有播放和暂停按钮。我试过像这样直接打开 URL:-

myView = (WebView) findViewById(R.id.webView);
    myView.getSettings().setJavaScriptEnabled(true);
    myView.loadUrl(myUrl);

但这不起作用。

【问题讨论】:

  • 最好将音频放在您的资产文件夹中以在 webview 中运行

标签: android


【解决方案1】:

这可以通过Javascript接口完成

创建类 WebInterface

public class WebInterface{
    Context mContext;

    WebInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void playSound(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public void pauseSound(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

在您的 WebView 类中

WebView browser;
browser=(WebView)findViewById(R.id.webkit);   
browser.getSettings().setJavaScriptEnabled(true);
browser.addJavascriptInterface(new WebInterface(this), "Android");        
browser.loadUrl("http://someurl.com");

在 HTML 代码中

<html>
<head>
<script type="text/javascript">
    function playSound(toast) {
        Android.showToast(toast);
    }

    function pauseSound(toast) {
        Android.showToast(toast);
    }
</script>
</head>
<body>
<input type="button" value="Say hello" onClick="playSound('Sound Played!')" />
<input type="button" value="Say hello" onClick="pauseSound('Sound Paused!')" />
</body>
</html>

【讨论】:

  • 为什么这个答案被否定了?这是最好的解决方法!
【解决方案2】:

试试看,这是有效的代码,

 WebView mWebView;
 mWebView = (WebView)findViewById(R.id.webView);

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebView.loadUrl("http://www.stephaniequinn.com/Music/Allegro%20from%20Duet%20in%20C%20Major.mp3");

    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());

我尝试了一个示例音频网址。它应该工作。

编码愉快..!!!

【讨论】:

    【解决方案3】:

    在 MyWebViewClient 中声明 MediaPLayer 的地方,添加以下内容...

    public class MyWebViewClient extends WebViewClient{
    
        public MediaPlayer mp;
        private Context context = null; // Add this line
    
        ...
    }
    

    然后给MyWebViewClient添加一个构造函数如下...

    public MyWebViewClient(Context context) {
        this.context = context;
    }
    

    然后在 shouldOverrideUrlLoading(...) 中获取您的资产,如下所示...

    AssetFileDescriptor afd = context.getAssets().openFd(url);
    

    在您的 MainActivity 中,通过传递 this(即 Activity 上下文)来设置 WebViewClient,如下所示...

    webMain.setWebViewClient(new MyWebViewClient(this));
    

    如果您需要更多帮助,请访问此链接 Android: Playing an Asset Sound Using WebView

    【讨论】:

      【解决方案4】:

      你想玩什么网址?如果是 HTML5 内容的复杂页面,可以使用crosswalk library 和XWalkView 代替WebView。但是这个库会大大增加你的 apk 大小。

      您也可以尝试将WebViewClient 设置为您的本机WebView 并处理onLoadResource() 方法。如果此方法接收到音频 URL,请将其定向到 Android MediaPlayer。

      【讨论】:

        猜你喜欢
        • 2012-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-15
        • 1970-01-01
        • 1970-01-01
        • 2017-12-23
        • 1970-01-01
        相关资源
        最近更新 更多