简体   繁体   中英

How to play audio on webview in android?

I want to play an audio from a URL on webview. I also want it to have play and pause button. I have tried opening the URL directly like this:-

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

But this isn't working.

This can be done through Javascript Interface

Create a Class 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();
    }
}

In your WebView class

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

In HTML code

<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>

Try, this is the working code,

 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());

i tried with one sample audio url. it should work.

Happy Coding..!!!

Where you declare your MediaPLayer in MyWebViewClient, add the following...

public class MyWebViewClient extends WebViewClient{

    public MediaPlayer mp;
    private Context context = null; // Add this line

    ...
}

Then add a constructor to MyWebViewClient as follows...

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

Then in shouldOverrideUrlLoading(...) get your assets as follows...

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

In your MainActivity set the WebViewClient by passing this (which is the Activity Context) as follows...

webMain.setWebViewClient(new MyWebViewClient(this));

If you want more help please visit this link Android: Playing an Asset Sound Using WebView

What URL you want to play? If it is complex page with HTML5 content, you can use crosswalk library and XWalkView instead of WebView . But this library will strongly increase your apk size.

Also you can try to set WebViewClient to your native WebView and handle onLoadResource() method. If this method receives audio URL, direct it to the Android MediaPlayer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM