为什么我不能通过html5与Android自动播放几个mp3文件?

时间:2014-06-25 13:01:05

标签: android html5

简单测试在这里:

http://jsfiddle.net/Z8z9Q/

  var audio        = new Audio();
  var audioElement = document.createElement('audio');

  function __log(e, data) {
    log.innerHTML += "\n" + e + " " + (data || '');
  }

  function play(text) {

      __log("TTS: "+text);
      audio.setAttribute("src", "http://translate.google.com/translate_tts?tl=en&q="+encodeURIComponent(text));
      audio.load();
      audio.play();
      __log("  -- Played");


  }


  setTimeout(function(){play('Hello World')},1000);
  setTimeout(function(){play('Hello World')},3000);
  setTimeout(function(){play('Hello World')},5000);
  setTimeout(function(){play('Hello World')},7000);
  setTimeout(function(){play('Hello World')},9000);

这对我的Chrome很好用,但我不能用Android播放几个mp3文件。但是同样的代码可以在一个文件中正常工作:

http://jsfiddle.net/D3gaC/2/

  var audio = new Audio();
  var audioElement = document.createElement('audio');

  function __log(e, data) {
    log.innerHTML += "\n" + e + " " + (data || '');
  }

  $(document).ready(function() {

    $('#subb').click(function (e) {
      //if (e.which == 13) {
        __log("TTS: "+$('#audiofield').val());
        audio.setAttribute("src", "http://translate.google.com/translate_tts?tl=en&q="+encodeURIComponent($('#audiofield').val()));
        audio.load();
        audio.play();
        __log("  -- Played");
        $('#audiofield').val("");

        //audioElement.setAttribute('src', "http://translate.google.com/translate_tts?tl=en&q="+encodeURIComponent($('#audiofield').val()));
        //audioElement.load()
        //audioElement.play(); 
        //__log("  -- Played method 2");
      //}
    });

  });

问题出在哪里?

1 个答案:

答案 0 :(得分:0)

遇到同样的问题......我认为在移动浏览器中,如果没有用户输入启动音频/视频播放也不会发生 - setTimeout可能不具备用户输入的条件。