【问题标题】:sound.pos() Howler method returns object but I need an integer or stringsound.pos() Howler 方法返回对象,但我需要一个整数或字符串
【发布时间】:2016-12-06 21:03:36
【问题描述】:

我正在尝试使用 Howler 将当前曲目时间输出到 HTML 页面,但目前它只是显示 [Object object]。相关代码:

$(document).ready(function() {
    var position = 0;
    var sound = new Howl({
        src: ["audiofiles/" + $(xml).find('mp3').text()],
        autoplay: true,
        loop: true,
        volume: 0.5,
            onload: function () {
                console.log("LOADED");
            }
        });
        // Output and update current track time as song plays
        position = sound.pos();
        document.getElementById("current_time").innerHTML = position;

如何以对输出到 HTML 页面有用的格式提取曲目时间信息?最好像这样:[mm/ss]。

我也试过sound.seek(),它也输出一个对象。

position 变量上调用toString() 会产生:[object Undefined] 当 HTML 加载到浏览器中时,并在其上调用 JSON.stringify() 只会导致我认为是空字符串(不会向网页输出任何内容)无论如何)。

【问题讨论】:

    标签: javascript jquery html casting howler.js


    【解决方案1】:

    您使用的似乎是 howler.js 2.0,它将 API 更改为使用 seek() 而不是 pos()。但是,这只是在那个时间点获得 seek 值,您有责任以您想要的速度在您的 HTML 中更新它(看看播放器example)。

    示例中的相关代码针对您的场景进行了调整:

    function step() {
      // Get the current position and format it however you would like.
      var seek = sound.seek() || 0;
      document.getElementById("current_time").innerHTML = self.formatTime(Math.round(seek));
    
      // Update the value on each frame.
      if (sound.playing()) {
        requestAnimationFrame(step);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-02
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 2013-11-12
      • 2011-11-15
      • 1970-01-01
      • 2017-07-11
      相关资源
      最近更新 更多