Soundmanager2:如何知道声音是否成功播放?

时间:2016-01-18 23:03:24

标签: javascript html5 soundmanager2

如何判断声音是否成功播放,并将代码置于处理错误状态?

s1 = soundManager.createSound({url:'http://www.example.com/sound1.mp3'});
s1.play();

我担心存在连接问题,或者找不到文件或大小为零。是否有我可以捕获的异常或事件被提出?

由于

1 个答案:

答案 0 :(得分:0)

创建声音时,可以为某些事件指定回调。在docs中,您可以找到进一步的解释。要向对象添加回调,请编写createSound函数,如下所示:

    s1 = soundManager.createSound({
      url: 'http://www.example.com/sound1.mp3',
      onconnect: function() { 
        // Fired when the file can be played and the connection is created
      },
      onload: function() { 
        // Fired when the file is completely loaded by the client
      },
      ondataerror: function() {
        // Fired when there is a Flash error
      },
      onfinish: function() {
        // Fired when the file has been completely reproduced
      }
   });

   s1.play();

onconnect 回调是一种在调用play()方法之前检测文件可以播放的方法,该方法不会引发可见错误。 ondataerror 它会告诉您播放音频文件时发生错误的时间。