W.M.
W.M.

Reputation: 776

How to access a MP3 file bundled with an Android application from within a WebView?

I have put a MP3 file under assets folder in my Android application and tried to access it from within a WebView (HTML file) using this JS code:

    var audio = new Audio('file:///android_asset/song.mp3')
    audio.play()

But this does not work. Any idea how to do this?

Upvotes: 0

Views: 60

Answers (1)

Cephas
Cephas

Reputation: 51

This Should help.

WebView  webview;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webview=(WebView)findViewById(R.id.webview);
    webview.setWebViewClient(new MyWebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("file:///android_asset/song.mp3");
}
 class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
             playaudio();
             return true;
    }


}
   private  void playaudio(){
       int i=R.raw.test;
        Log.v("id of file",""+i);
        if(i!=0){
        MediaPlayer player = new MediaPlayer().create(getBaseContext(),i);; 
        player.setVolume(0.9f, 0.9f);
        player.start();
        }
   }

Upvotes: 1

Related Questions