简体   繁体   English

如何在android上的webview中从资产文件夹中播放mp3文件?

[英]how to play mp3 file from asset folder in webview on android?

I am new android developer , trying to play local sdcard mp3 file in WebView . 我是新的android开发人员,试图在WebView中播放本地sdcard mp3文件。 help me. 帮我。

I want to add sound in android web view application. 我想在android web view应用程序中添加声音。 ( The sound should be play when click a button in the screen.) My html application worked in the browser. (单击屏幕上的按钮时应播放声音。)我的​​html应用程序在浏览器中工作。 But not worked in android.I don't know why? 但是没有在android上工作。我不知道为什么?

WebView  webview;
 @Override
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webview=(WebView)findViewById(R.id.webview);
        et_Url=(EditText)findViewById(R.id.et_Url);
        btn_Go=(Button)findViewById(R.id.btn_Go);

        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("file:///android_asset/test.html"); 

 }
 //asset file
   test.html
   test.mp3
  //html source code
  <audio id="sample" src="file:///android_asset/test.mp3" controls preload></audio>
  <a href="javascript:playSegment(0.5);">Play2</a>
  <script>
  var audio = document.getElementById('sample');
  var segmentEnd;
  function playSegment(startTime, endTime){
    segmentEnd = endTime;
    audio.currentTime = startTime;
    audio.play();
  }
  </script>

I am solve my problem this way .. I hope this will be helpful to you... 我这样解决了我的问题..我希望这会对你有所帮助......

//asset folder
   test.html
   // test.html html source code 
     <a href="test.mp3">Play</a>
//raw folder contains following file
  test.mp3
    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/test.html");
    }
     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();
            }
       }

I hope this answer helpful for you 我希望这个答案对你有帮助

view.loadDataWithBaseURL("file:///android_res/raw/", javascrips, "text/html",
                "UTF-8", null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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