admin管理员组文章数量:1317565
I'm trying to very quickly port over an html application with plays sound (with soundmanager2) to a native android app using WebView. From my research, I haven't seen any evidence that sound, such as mp3s, can be played via javascript or any other methods using WebView.
My goal now is to quickly port over the html app and then eventually create a truely native application without using WebViews.
Please let me know if anyone has been able to successfully implement playing audio using WebViews and Javascript or if this is just not possible at this time.
Thank you.
I'm trying to very quickly port over an html application with plays sound (with soundmanager2) to a native android app using WebView. From my research, I haven't seen any evidence that sound, such as mp3s, can be played via javascript or any other methods using WebView.
My goal now is to quickly port over the html app and then eventually create a truely native application without using WebViews.
Please let me know if anyone has been able to successfully implement playing audio using WebViews and Javascript or if this is just not possible at this time.
Thank you.
Share Improve this question asked Sep 25, 2010 at 19:41 ronron 891 gold badge1 silver badge7 bronze badges 1- I think a found a nice workaround for it. Check this out: stackoverflow./a/40634355/3866399 – Rodrigo Borba Commented Nov 16, 2016 at 14:19
2 Answers
Reset to default 2Play around with this:
if(cm.message().contains("play sound")) {
//String[] temp = cm.message().split("#");
AudioManager audiomanager= (AudioManager) getSystemService(AUDIO_SERVICE);
float actualvolume = (float) audiomanager.getStreamVolume (AudioManager.STREAM_MUSIC);
float maxVolume= (float) audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualvolume/maxVolume;
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 54, 1f);
Log.e("Test", "Played sound");
}
} else if(cm.message().contains("stop sound")) {
soundPool.stop(1);
Log.e("Test","stoped");
}
myWebView.loadUrl("file:///android_asset/timer.html");
soundID = soundPool.load(this, R.raw.sound1,1);
Would this work for you?
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".ogg")){
Log.d(TAG, "Reproducir archivo OGG");
Uri tempPath = Uri.parse(url);
MediaPlayer player = MediaPlayer.create(WebViewVideo.this, tempPath);
player.start();
return true;
}else{
return super.shouldOverrideUrlLoading(view, url);
}
}
});
If you really want to use Javascript, you can try with document.open() or something like that, I haven't tried so, but I guess it would work the same way.
By the way, this is the case that you want to play that audio on background, if you want to show the actual player, I think you have to implement the View by yourself.
And if you were considering html5 audio tag, forget it. AFAIK Froyo doesn't support it in any way.
I hope this helps and it's not too late.
Regards.
本文标签: Android WebView playing audio with javascriptStack Overflow
版权声明:本文标题:Android WebView playing audio with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742002029a2411208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论