javascript - 如何通过函数获取 audio.duration 值

标签 javascript jquery audio html5-audio

我是我的音频播放器,我需要获取音轨的持续时间。我需要一个函数来获取音频的 src 并返回其持续时间。这是我正在尝试但不起作用的方法:

function getDuration(src){
    var audio = new Audio();
    audio.src = "./audio/2.mp3";
    var due;
    return getVal(audio);
}
function getVal(audio){
    $(audio).on("loadedmetadata", function(){
        var val = audio.duration;
        console.log(">>>" + val);
        return val;
    });
}

我试图拆分成两个函数,但它不起作用。如果能像工作函数那样就好了。

有什么想法吗?

最佳答案

因为您依赖事件来触发,所以您不能在 getDuration 或 getVal 中返回值

相反,你想使用回调函数,像这样(回调)

这个例子假设你想把持续时间放到这样写的跨度中

<span id="duration"></span>

function getDuration(src, cb) {
    var audio = new Audio();
    $(audio).on("loadedmetadata", function(){
        cb(audio.duration);
    });
    audio.src = src;
}
getDuration("./audio/2.mp3", function(length) {
    console.log('I got length ' + length);
    document.getElementById("duration").textContent = length;
});

任何需要“知道”长度的代码都应该在回调函数中(console.log 所在的位置)


使用 promise

function getDuration(src) {
    return new Promise(function(resolve) {
        var audio = new Audio();
        $(audio).on("loadedmetadata", function(){
            resolve(audio.duration);
        });
        audio.src = src;
    });
}
getDuration("./audio/2.mp3")
.then(function(length) {
    console.log('I got length ' + length);
    document.getElementById("duration").textContent = length;
});

使用事件 - 注意 'myAudioDurationEvent' 显然(几乎)可以是任何你想要的

function getDuration(src, obj) {
    return new Promise(function(resolve) {
        var audio = new Audio();
        $(audio).on("loadedmetadata", function(){
            var event = new CustomEvent("myAudioDurationEvent", {
                detail: {
                    duration: audio.duration,

                }
            });
            obj.dispatchEvent(event);
        });
        audio.src = src;
    });
}
var span = document.getElementById('xyz'); // you'll need to provide better logic here
span.addEventListener('myAudioDurationEvent', function(e) {
    span.textContent = e.detail.duration;
});
getDuration("./audio/2.mp3", span);

不过,这也可以通过回调或 promise 类似地完成,方法是在这些解决方案中将目标传递给修改后的 getDuration 函数 - 我关于使用事件监听器的观点更合适,例如,如果一个跨度多次更新持续时间- 此解决方案仍然只对每个跨度执行一次,因此可以使用其他方法轻松实现


given the new information in the comments for this answer, I believe this to be the better solution

function getDuration(src, destination) {
    var audio = new Audio();
    $(audio).on("loadedmetadata", function(){
        destination.textContent = audio.duration;
    });
    audio.src = src;
}

然后像这样根据需要调用 getDuration

var span = createOrGetSomeSpanElement();
getDuration("./audio/2.mp3", span);

createOrGetSomeSpanElement 返回要在 getDuration 函数中使用的目标元素 - 如何完成取决于您,当您在循环中创建播放列表时,我'我猜你创建了一些元素来接收已经创建的音频长度——有时很难回答一半的问题

关于javascript - 如何通过函数获取 audio.duration 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34647536/

相关文章:

javascript - hide dropdown on 'body' 点击产生需要双击再次显示

javascript - 在 Javascript 中停止父处理程序调用

jquery - jQuery cookie-音频

windows - BASIC - 在按键上激活麦克风录音?

javascript - 如何理解客户端脚本中时区的时钟时间

javascript - 在 Backbone.js 中设置自定义 header 不起作用

javascript - 在没有标准键的情况下过滤 Javascript 对象数组的最有效方法

javascript - 如何加深html表格中重叠单元格的颜色

javascript - dataTable fn用新值更新行

java - Android的音频记录不起作用