naveenballa
naveenballa

Reputation: 33

Not able to play audio using javascript in webview in android

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

Upvotes: 3

Views: 4685

Answers (2)

OJay
OJay

Reputation: 1239

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

Hope that helps.

Upvotes: 0

Vaibs_Cool
Vaibs_Cool

Reputation: 6158

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

Upvotes: 1

Related Questions