在自定义 chromecast 接收器中播放来自 javascript 的音频的最佳音频文件格式是什么
What is the best audio file format to play audio from javascript in custom chromecast receiver
我开发了一个简单的自定义 chrome游戏接收器。
我在其中播放来自接收器 javascript 的短音,使用:
this.bounceSound = new Audio("paddle.ogg");
在游戏加载时创建音频对象,然后使用:
this.bounceSound.play();
在游戏中需要时播放声音。
这在 chrome 我的笔记本电脑上工作正常,但是当 运行 我的 chromecast 中的接收器时,一些声音不播放,其他声音延迟。
这可能是我为音频文件选择的声音格式 (.ogg) 有问题吗?
如果不是,还有什么问题。
他们在声音文件的细节(频率、比特深度等)方面有什么最佳实践吗?
谢谢
您应该在开始游戏之前预先下载声音。另请注意,这些声音随后将存储在内存中,而 Chromecast 的内存非常有限。确保这些声音很小并且都适合记忆。
仅作记录,以避免将来其他开发人员试图同时加载和播放多个短声音时造成混淆:
On Chromecast, the HTML video and audio tags can only support a
single active media element at a time.
(来源:https://plus.google.com/+LeonNicholls/posts/3Fq5jcbxipJ - 请务必阅读其余部分,其中还包含有关限制的重要信息)
只会加载一个音频元素,其他元素会得到错误代码 4(至少在我的调试会话期间是这种情况)。加载和播放几个短声音的正确方法是使用网络音频 API,正如 Leon Nicholls 在他的 Google+ post 中所解释的,我在上面链接。
简单网络音频API包装器
我基于 Web Audio API:
为 JavaScript 中的 HTMLAudioElement 创建了一个粗略的替代品
function WebAudio(src) {
if(src) this.load(src);
}
WebAudio.prototype.audioContext = new AudioContext;
WebAudio.prototype.load = function(src) {
if(src) this.src = src;
console.log('Loading audio ' + this.src);
var self = this;
var request = new XMLHttpRequest;
request.open("GET", this.src, true);
request.responseType = "arraybuffer";
request.onload = function() {
self.audioContext.decodeAudioData(request.response, function(buffer) {
if (!buffer) {
if(self.onerror) self.onerror();
return;
}
self.buffer = buffer;
if(self.onload)
self.onload(self);
}, function(error) {
self.onerror(error);
});
};
request.send();
};
WebAudio.prototype.play = function() {
var source = this.audioContext.createBufferSource();
source.buffer = this.buffer;
source.connect(this.audioContext.destination);
source.start(0);
};
可以这样使用:
var audio1 = new WebAudio('sounds/sound1.ogg');
audio1.onload = function() {
audio1.play();
}
var audio2 = new WebAudio('sounds/sound2.ogg');
audio2.onload = function() {
audio2.play();
}
我开发了一个简单的自定义 chrome游戏接收器。
我在其中播放来自接收器 javascript 的短音,使用:
this.bounceSound = new Audio("paddle.ogg");
在游戏加载时创建音频对象,然后使用:
this.bounceSound.play();
在游戏中需要时播放声音。
这在 chrome 我的笔记本电脑上工作正常,但是当 运行 我的 chromecast 中的接收器时,一些声音不播放,其他声音延迟。
这可能是我为音频文件选择的声音格式 (.ogg) 有问题吗?
如果不是,还有什么问题。
他们在声音文件的细节(频率、比特深度等)方面有什么最佳实践吗?
谢谢
您应该在开始游戏之前预先下载声音。另请注意,这些声音随后将存储在内存中,而 Chromecast 的内存非常有限。确保这些声音很小并且都适合记忆。
仅作记录,以避免将来其他开发人员试图同时加载和播放多个短声音时造成混淆:
On Chromecast, the HTML video and audio tags can only support a single active media element at a time.
(来源:https://plus.google.com/+LeonNicholls/posts/3Fq5jcbxipJ - 请务必阅读其余部分,其中还包含有关限制的重要信息)
只会加载一个音频元素,其他元素会得到错误代码 4(至少在我的调试会话期间是这种情况)。加载和播放几个短声音的正确方法是使用网络音频 API,正如 Leon Nicholls 在他的 Google+ post 中所解释的,我在上面链接。
简单网络音频API包装器
我基于 Web Audio API:
为 JavaScript 中的 HTMLAudioElement 创建了一个粗略的替代品function WebAudio(src) {
if(src) this.load(src);
}
WebAudio.prototype.audioContext = new AudioContext;
WebAudio.prototype.load = function(src) {
if(src) this.src = src;
console.log('Loading audio ' + this.src);
var self = this;
var request = new XMLHttpRequest;
request.open("GET", this.src, true);
request.responseType = "arraybuffer";
request.onload = function() {
self.audioContext.decodeAudioData(request.response, function(buffer) {
if (!buffer) {
if(self.onerror) self.onerror();
return;
}
self.buffer = buffer;
if(self.onload)
self.onload(self);
}, function(error) {
self.onerror(error);
});
};
request.send();
};
WebAudio.prototype.play = function() {
var source = this.audioContext.createBufferSource();
source.buffer = this.buffer;
source.connect(this.audioContext.destination);
source.start(0);
};
可以这样使用:
var audio1 = new WebAudio('sounds/sound1.ogg');
audio1.onload = function() {
audio1.play();
}
var audio2 = new WebAudio('sounds/sound2.ogg');
audio2.onload = function() {
audio2.play();
}