Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

android - Sound in Webview

I have a problem with the Playback of Sounds in Webview. I want to play the Sound in the background what is opened by klicking a button. I use at the moment folowwing script, but when i click to the button a new window is opened.

package C.K;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

final class MyWebViewClient extends WebViewClient {
    @Override     
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {         
        if (url.endsWith(".mp3"))  {             
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "audio/*");
            view.getContext().startActivity(intent);

            return true;
            } else {
                return super.shouldOverrideUrlLoading(view, url);
                }     } } 

Who can help me??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The code you are writting is to open a new window whenever you will click on link which has .mp3 files in web page

where are your files stored in the app assets or raw or its a url

this is the code for playing file from webview without openning a new window the audio files are stored in raw folder

       @Override     
         public boolean shouldOverrideUrlLoading(WebView view, String url)
        {         
              if (url.endsWith(".mp3"))  {       
              String temp1=url.replace(".mp3", "");      
              playaudio(temp1);

              return true;
        }      
      return true;
 } 


       private  void playaudio(String url){
        String s=url;
        int i=getResources().getIdentifier(s,"raw",getPackageName());
        Log.v("name of file",""+s);
        Log.v("id of file",""+i);
        if(i!=0){
        MediaPlayer player = new MediaPlayer().create(getBaseContext(),i);; 
        player.setVolume(0.9f, 0.9f);
        player.start();
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...