Android: playing sound using WebView

I am trying to play sound from a resource folder when a user clicks on a tag in my WebView application. I found that I can use the new WebViewClient extension class to detect the link extension, and if it is an mp3 file, it can play it through Audio Player by default. But I want him to play in his activities, not starting a new activity.

I used the following links as links: Sound in a web browser and Error creating MediaPlayer with Uri or file in assets

But that did not work.

I tried using the following codes for this:

package com.rangga.test.webview.sound; import android.content.Intent; import android.content.res.AssetFileDescriptor; import android.media.MediaPlayer; import android.net.Uri; import android.webkit.WebView; import android.webkit.WebViewClient; public class MyWebViewClient extends WebViewClient{ public MediaPlayer mp; @Override public boolean shouldOverrideUrlLoading(WebView view, String url){ if(url.endsWith(".mp3")){ mp = new MediaPlayer(); AssetFileDescriptor afd = getAssets().openFd(url); mp.setDataSource(afd.getFileDescriptor()); mp.start(); return true; }else{ return true; } } } 

I get an undefined method error on getAssets (), I tried it and it says something about passing context. I am very new to Android and object-oriented programming, so I wonder if this is the right thing to do, and if there is a simple explanation of what Context is. Thanks.

Below are my complete codes:

MainActivity.class

 package com.rangga.test.webview.sound; import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; public class MainActivity extends Activity { WebView webMain; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webMain = (WebView) findViewById(R.id.webMain); webMain.loadUrl("file:///android_asset/webpages/test.html"); webMain.setWebViewClient(new MyWebViewClient()); } } 

EDIT

So the codes work thanks to https://stackoverflow.com/users/488241/squonk , but now there is another problem, I updated MyWebViewClient in the following:

 package com.rangga.test.webview.sound; import java.io.IOException; import android.content.Context; import android.content.res.AssetFileDescriptor; import android.media.MediaPlayer; import android.webkit.WebView; import android.webkit.WebViewClient; public class MyWebViewClient extends WebViewClient{ public MediaPlayer mp; private Context context = null; public MyWebViewClient(Context c){ this.context = c; } @Override public boolean shouldOverrideUrlLoading(WebView view, String url){ if(url.endsWith(".mp3")){ mp = new MediaPlayer(); try { AssetFileDescriptor afd = context.getAssets().openFd(url); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); mp.prepare(); mp.start(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; }else{ return true; } } } 

But I get java.io.FileNotFoundException, the path seems to be correct. I put 2 mp3 files in assets / web pages / sounds / 1.mp3 and assets / web pages / 1.mp3 and used the following html codes to check it:

 <html> <head> <title>Test Audio</title> </head> <body> <a href="file:///android_asset/webpages/sounds/1.mp3">File Asset</a><br/> <a href="1.mp3">Same Folder</a> </body> </html> 

But I see the following error in LogCat:

 06-11 06:20:37.955: W/System.err(377): java.io.FileNotFoundException: file:///android_asset/webpages/1.mp3 06-11 06:20:37.966: W/System.err(377): at android.content.res.AssetManager.openAssetFd(Native Method) 06-11 06:20:37.974: W/System.err(377): at android.content.res.AssetManager.openFd(AssetManager.java:314) 06-11 06:20:37.974: W/System.err(377): at com.rangga.test.webview.sound.MyWebViewClient.shouldOverrideUrlLoading(MyWebViewClient.java:30) 06-11 06:20:37.985: W/System.err(377): at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:193) 06-11 06:20:38.000: W/System.err(377): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:304) 06-11 06:20:38.005: W/System.err(377): at android.os.Handler.dispatchMessage(Handler.java:99) 06-11 06:20:38.005: W/System.err(377): at android.os.Looper.loop(Looper.java:123) 06-11 06:20:38.014: W/System.err(377): at android.app.ActivityThread.main(ActivityThread.java:4363) 06-11 06:20:38.014: W/System.err(377): at java.lang.reflect.Method.invokeNative(Native Method) 06-11 06:20:38.024: W/System.err(377): at java.lang.reflect.Method.invoke(Method.java:521) 06-11 06:20:38.024: W/System.err(377): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 06-11 06:20:38.024: W/System.err(377): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 06-11 06:20:38.054: W/System.err(377): at dalvik.system.NativeStart.main(Native Method) 

Is there something wrong with the name of the file? Thanks before.

solvable

thanks to put together the sentences, I tried the following codes to make it work and it worked well, thanks squonk!

 package com.rangga.test.webview.sound; import java.io.IOException; import android.content.Context; import android.content.res.AssetFileDescriptor; import android.media.MediaPlayer; import android.util.Log; import android.webkit.WebView; import android.webkit.WebViewClient; public class MyWebViewClient extends WebViewClient{ public MediaPlayer mp; private Context context = null; public MyWebViewClient(Context c){ this.context = c; mp = new MediaPlayer(); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url){ if(url.endsWith(".mp3")){ url = url.replace("file:///android_asset/webpages/", ""); Log.i("MyWebViewClient", url); try { AssetFileDescriptor afd = context.getAssets().openFd(url); mp = new MediaPlayer(); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); mp.prepare(); mp.start(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; }else{ return true; } } } 

And html to check it out:

 <html> <head> <title>Test Audio</title> </head> <body> <a href="webpages/1.mp3">File Asset</a><br/> </body> </html> 

Although I made a mistake if I repeatedly click on it, but another question: D How to close one question ?. > & L;

+8
android webview audio android-assets
source share
1 answer

If 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 the 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 MainActivity set WebViewClient by passing this (which is an Activity Context ) as follows:

 webMain.setWebViewClient(new MyWebViewClient(this)); 

Also you should return false from shouldOverrideUrlLoading(...) if url ends with .mp3. This shows that your WebView processing things, and the host application should not launch a web browser instance.

EDIT: To play audio files from an asset catalog, you need to set the data source differently - see the accepted answer play-audio-file-from-the-assets-directory .

You must also call mp.prepare() before calling mp.start() .

+3
source share

All Articles