[英]Sketch JS, how to use AWS pre-signed URL on audio tag?
快速瀏覽
在此示例中,我使用的是Sketch.js插件。 我也想使用我的預簽名URL,但是它們不起作用。 到期時間設置得足夠長(1天),因此JS本身存在問題。
我有一個S3存儲桶,用於存儲一些受公眾保護的音樂。 使用官方的AWS開發工具包,我可以生成以下網址:
https://d225******.cloudfront.net/song.m4a?Expires=1493381986&Signature=***&Key-Pair-Id=***
我在網站上使用預簽名的URL沒問題,但是在此腳本中不起作用:
<script>
var ALPHA, AudioAnalyser, COLORS, MP3_PATH, NUM_BANDS, NUM_PARTICLES, Particle, SCALE, SIZE, SMOOTHING, SPEED, SPIN;
MP3_PATH = 'my_presigned_url';
AudioAnalyser = (function() {
AudioAnalyser.AudioContext = self.AudioContext || self.webkitAudioContext;
AudioAnalyser.enabled = AudioAnalyser.AudioContext != null;
function AudioAnalyser(audio, numBands, smoothing) {
var src;
this.audio = audio != null ? audio : new Audio();
this.numBands = numBands != null ? numBands : 256;
this.smoothing = smoothing != null ? smoothing : 0.3;
if (typeof this.audio === 'string') {
src = this.audio;
this.audio = new Audio();
this.audio.crossOrigin = "anonymous";
this.audio.controls = true;
this.audio.src = src;
}
this.context = new AudioAnalyser.AudioContext();
this.jsNode = this.context.createScriptProcessor(2048, 1, 1);
this.analyser = this.context.createAnalyser();
this.analyser.smoothingTimeConstant = this.smoothing;
this.analyser.fftSize = this.numBands * 2;
this.bands = new Uint8Array(this.analyser.frequencyBinCount);
this.audio.addEventListener('canplay', (function(_this) {
return function() {
_this.source = _this.context.createMediaElementSource(_this.audio);
_this.source.connect(_this.analyser);
_this.analyser.connect(_this.jsNode);
_this.jsNode.connect(_this.context.destination);
_this.source.connect(_this.context.destination);
return _this.jsNode.onaudioprocess = function() {
_this.analyser.getByteFrequencyData(_this.bands);
if (!_this.audio.paused) {
return typeof _this.onUpdate === "function" ? _this.onUpdate(_this.bands) : void 0;
}
};
};
})(this));
}
AudioAnalyser.prototype.start = function() {
return this.audio.play();
};
AudioAnalyser.prototype.stop = function() {
return this.audio.pause();
};
return AudioAnalyser;
})();
Sketch.create({
particles: [],
setup: function() {
var analyser, error, i, intro, j, particle, ref, warning, x, y;
for (i = j = 0, ref = NUM_PARTICLES - 1; j <= ref; i = j += 1) {
x = random(this.width);
y = random(this.height * 2);
particle = new Particle(x, y);
particle.energy = random(particle.band / 256);
this.particles.push(particle);
}
if (AudioAnalyser.enabled) {
try {
analyser = new AudioAnalyser(MP3_PATH, NUM_BANDS, SMOOTHING);
analyser.onUpdate = (function(_this) {
return function(bands) {
var k, len, ref1, results;
ref1 = _this.particles;
results = [];
for (k = 0, len = ref1.length; k < len; k++) {
particle = ref1[k];
results.push(particle.energy = bands[particle.band] / 256);
}
return results;
};
})(this);
analyser.start();
document.getElementById('player-container').appendChild(analyser.audio);
document.getElementsByTagName("audio")[0].setAttribute("id", "dy_wowaudio");
intro = document.getElementById('intro');
intro.style.display = 'none';
} catch (_error) {
error = _error;
}
}
}
});
// generated by coffee-script 1.9.2
</script>
該腳本可以很好地工作(如您在上面的示例中看到的),沒有預簽名的url,那么如何在AudioAnalyser
函數中使用我的預簽名的url ?
我已經看到html5視頻標簽發出了多個請求。 我假設要獲取一些元數據,例如播放長度和視頻的第一幀用作縮略圖。 您可以嘗試使用preload屬性來防止這種情況。
具體來說,如果剪輯很小,則preload="auto"
可能就是您需要的一切。 我認為,如果瀏覽器必須發出跟蹤請求,您將會遇到困難。 這是一些相關信息
我認為可能更可靠地解決此問題的另一種方法是根據需要生成臨時憑據。
有關更多信息,請參閱文檔:- 請求臨時憑證 - 使用臨時憑證訪問資源
與JS軟件包結合使用,以簽署諸如雙筒望遠鏡/ aws-sigv4之類的AWS請求或復制在瀏覽器中執行此操作的其他人。
共享您的瀏覽器錯誤消息(如果有)。 這可能與S3存儲桶數據的交叉原點相關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.