我正在尝试使用$ .get jQuery函数从api获取音频URL。但是,如果我呼叫$ .get并在$ .get.done内部调用,我将无法播放音频。
这是我的html:
<audio id="blast"></audio>
<button class="blasterTrigger">Shoot!</button>
这里正在使用JS(如果我在click函数内部直接传递了音频下载URL):
$(".blasterTrigger").click(function () {
var blast = $("#blast");
blast.attr("src", "https://www.mp3factory.net/index.php?route=musics/download/download/44316747367616");
blast[0].play();
});
但是此JS无法正常工作(通过带有$ .get函数的API传递音频下载URL)
$(".blasterTrigger").click(function () {
var music_id = $(this).data('id');
var url = "https://www.mp3factory.net/index.php?route=musics/play/play/44316747367616";
var jqxhr = $.get( url, null, function() {}, 'json');
var play_url = null;
jqxhr.done(function(data) {
if(data.status == 'OK')
{
var play_url = data.play_url;
var blast = $("#blast");
blast.attr("src", play_url);
blast[0].play();
}
if(data.status == 'fail')
{
alert('An unknown error occured');
}
});
jqxhr.fail(function() {
alert('An error occured while processing your request. Please try again later...');
});
});
这是API网址:https://www.mp3factory.net/index.php?route=musics/play/play/44316747367616
这是直接音频URL:https://www.mp3factory.net/index.php?route=musics/download/download/44316747367616