本文介绍了无法在android的webview中使用javascript播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Android手机上,当我在html文件中使用以下代码时,正在播放音频,但是当我删除自动播放"属性时,无法在webview上播放音频.有人对此有解决方案吗?
On Android phone, audio is playing when i used the following code in html file, but when i remove the "autoplay" attribute i am unable to play the audio on webview. Does anyone has solution for this?
<audio id="audiotag1" style="display:none" preload="auto" hidden='true' autoplay>
</audio>
<script type="text/javascript">
var audio1 = document.getElementById("audiotag1");
audio1.src = "file:///sdcard/Music/intro.mp3";
audio1.type = "audio/mpeg";
audio1.play();
</script>
预先感谢
推荐答案
尝试使用此代码段
//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);
查看此链接
http://docs.phonegap.com/en/2.9. 0/cordova_media_media.md.html
这篇关于无法在android的webview中使用javascript播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!