HTML5 audio not playing multiple times in Android 4.0.4 device Native Browser

后端 未结 3 992
盖世英雄少女心
盖世英雄少女心 2021-01-30 18:02

I am currently working on an HTML5 project.

There is an issue on playing same audio file multiple times in the same page in Andr

相关标签:
3条回答
  • 2021-01-30 18:44

    This works for me:

    var movie_audio = document.getElementById('movie_audio');  
    movie_audio.addEventListener('ended', function(){  
       movie_audio.load(); 
    });
    
    0 讨论(0)
  • 2021-01-30 18:55

    I got a working solution..

    The problem was in Android 4.0.4 browser when audio is played once(ended) then the seek time will not be reset to starting position, it will stay at the end itself. So by setting currentTime = 0 after the audio ended will reset it to starting position and in this way we can play the same audio as many times as we want with out any page refresh.
    Tested working perfectly in all devices.

    HTML Code:

    <audio id="movie_audio">
      <source src="my_audio_location/movie_audio.mp3" type='audio/mpeg; codecs="mp3"' />
      <source src="my_audio_location/movie_audio.ogg" type='audio/ogg; codecs="vorbis"' />  
    </audio>
    

    JavaScript Code:

    var movie_audio = document.getElementById('movie_audio');  
    movie_audio.addEventListener('ended', function(){  
      movie_audio.currentTime = 0;  
    }
    
    0 讨论(0)
  • 2021-01-30 18:57

    If the above solution doesn't work for you, try this one.

    audio_element.addEventListener('timeupdate', function() {
     if (audio_element.currentTime >= ( audio_element.duration - 0.3 ) ) {
      audio_element.currentTime = 0;
      audio_element.pause();
     }
    }, false);  
    

    Note:We are setting the seekbar to starting position just before the audio end.

    0 讨论(0)
提交回复
热议问题