Not able to play audio using javascript in webview in android

若如初见. 提交于 2019-12-22 13:04:59

问题


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>

Thanks in advance


回答1:


Try using this code snippet

    //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);

Look at this link

http://docs.phonegap.com/en/2.9.0/cordova_media_media.md.html




回答2:


You might consider using an audio library like SoundJS to make this easier for you.

Hope that helps.



来源:https://stackoverflow.com/questions/19157876/not-able-to-play-audio-using-javascript-in-webview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!