Reputation: 2119
I am new android developer , trying to play local sdcard mp3 file in WebView . help me.
I want to add sound in android web view application. ( The sound should be play when click a button in the screen.) My html application worked in the browser. But not worked in android.I don't know why?
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>
Upvotes: 3
Views: 3717
Reputation: 2119
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();
}
}
Upvotes: 2
Reputation: 1611
I hope this answer helpful for you
view.loadDataWithBaseURL("file:///android_res/raw/", javascrips, "text/html",
"UTF-8", null);
Upvotes: 0