gpt4 book ai didi

javascript - 强制加载 JS 中的音频库

转载 作者:行者123 更新时间:2023-12-03 11:17:56 26 4
gpt4 key购买 nike

我想获取音频长度而不必播放然后暂停然后再次播放以获得音频长度/持续时间?

调用的函数将是:this.createRangeElement

但出于某种原因它输出“NaN”,那么我该如何强制渲染音频?

function get_uai(){ return new uai(); }
function uai(){
var AE = new Audio();
var played = false;

this.src = "";
this.set_src = function(){ AE.src = this.src; AE.load(); }


this.play = function(){
if(played == true){
AE.play();
}else{
AE.src = this.src;
played = true;
AE.play();
}
};

this.pause = function(){
AE.pause();
}

this.stop = function(){
window.aui = undefined;
}


this.createRangeElement = function(){
var min = "0";
AE.load();
var max = AE.duration;
console.log(max);
}

}


// Getting the UAI API
var aud = get_uai();

// Setting the source
aud.src = "http://crossorigin.me/https://s3-us-west-2.amazonaws.com/s.cdpn.io/1715/the_xx_-_intro.mp3";
aud.set_src();

// Creating a range element
aud.createRangeElement();

// Playing the sound
//aud.play()
<html>
<head>
<title>Music Player</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
</head>
<body>
<em class="fa fa-pause" onclick="aud.pause();"></em>
<em class="fa fa-play" onclick="aud.play();"></em>
</body>
</html>

最佳答案

您可以使用 oncanplaythrough检测音频持续时间数据是否可用的事件。

function get_uai() {
return new uai();
}

function uai() {
var AE = new Audio();
var played = false;

this.src = "";
this.set_src = function() {
AE.src = this.src;
AE.load();
}

this.play = function() {
if (played == true) {
AE.play();
} else {
AE.src = this.src;
played = true;
AE.play();
}
};

this.pause = function() {
AE.pause();
}

this.stop = function() {
window.aui = undefined;
}


this.createRangeElement = function() {
var min = "0";
AE.load();
AE.oncanplaythrough = function() {
var max = AE.duration;
console.log(max);
}
}

}

var aud = get_uai();
aud.src = "http://crossorigin.me/https://s3-us-west-2.amazonaws.com/s.cdpn.io/1715/the_xx_-_intro.mp3";
aud.set_src();
aud.createRangeElement();
<html>
<head>
<title>Music Player</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
</head>
<body>
<em class="fa fa-pause" onclick="aud.pause();"></em>
<em class="fa fa-play" onclick="aud.play();"></em>
</body>
</html>

关于javascript - 强制加载 JS 中的音频库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40557808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com