【发布时间】:2015-11-24 08:15:40
【问题描述】:
好的,所以我让这门课工作并播放声音。它突然停止播放声音。如果我在 chromecast 调试器中调试它,当我创建它的一个 istance 并调用 init() 函数时,所有数据成员都会被填充。
var audioplayer = new cast.AudioPlayer();
audioplayer.init();
然后在我确定 .wav 文件已加载后,我调用我的播放方法
audioplayer.play(cast.AudioPlayer.welcome);//pass index of buffer
这是课程
var cast = window.cast || {};
(function() {
'use strict';
AudioPlayer.applause = 0;
AudioPlayer.ding = 1;
AudioPlayer.buzzer = 2;
AudioPlayer.sigh = 3;
AudioPlayer.welcome = 4;
function AudioPlayer() {
try {
// Fix up for prefixing
window.AudioContext = window.AudioContext
|| window.webkitAudioContext;
this.context = new AudioContext();
} catch (e) {
console.log('Web Audio API is not supported in this browser');
}
this.soundBuffer = [];
this.loaded = false;
this.sources = [];
}
AudioPlayer.prototype = {
play : function(index){
this.sources[index].start(0);//play then reload the buffer to reduce latency between user action and sound playing
this.sources[index] = this.context.createBufferSource();
this.sources[index].buffer = this.soundBuffer[index];
this.sources[index].connect(this.context.destination);
},
init : function() {
// Fix up prefixing
window.AudioContext = window.AudioContext
|| window.webkitAudioContext;
this.context = new AudioContext();
var bufferLoader = new BufferLoader(this.context, this, [
'./sounds/applause.wav',
'./sounds/ding.wav',
'./sounds/buzzer.wav',
'./sounds/sigh.wav',
'./sounds/welcome.wav',],
this.finishedLoading);
bufferLoader.load();
},
//buffer up the sounds so they are immediately ready to play
finishedLoading : function(bufferList) {
this.sources[0] = this.context.createBufferSource();
this.sources[1] = this.context.createBufferSource();
this.sources[2] = this.context.createBufferSource();
this.sources[3] = this.context.createBufferSource();
this.sources[4] = this.context.createBufferSource();
this.sources[0].buffer = bufferList[0];
this.sources[1].buffer = bufferList[1];
this.sources[2].buffer = bufferList[2];
this.sources[3].buffer = bufferList[3];
this.sources[4].buffer = bufferList[4];
this.sources[0].connect(this.context.destination);
this.sources[1].connect(this.context.destination);
this.sources[2].connect(this.context.destination);
this.sources[3].connect(this.context.destination);
this.sources[4].connect(this.context.destination);
this.soundBuffer = bufferList;
this.loaded = true;
console.log('Sounds Loaded!');
}
};
// Exposes public functions and APIs
cast.AudioPlayer = AudioPlayer;
})();
这里是缓冲区加载器,抱歉这两种不同类型的封装。
function BufferLoader(context, cbObj, urlList, callback) {
this.context = context;
this.urlList = urlList;
this.onload = callback;
this.bufferList = new Array();
this.loadCount = 0;
this.callBackObj = cbObj;
}
BufferLoader.prototype.loadBuffer = function(url, index) {
// Load buffer asynchronously
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";
var loader = this;
request.onload = function() {
// Asynchronously decode the audio file data in request.response
loader.context.decodeAudioData(
request.response,
function(buffer) {
if (!buffer) {
console.log('error decoding file data: ' + url);
return;
}
loader.bufferList[index] = buffer;
if (++loader.loadCount == loader.urlList.length)
loader.onload.apply(loader.callBackObj, [loader.bufferList]);
},
function(error) {
console.error('decodeAudioData error', error);
}
);
}
request.onerror = function() {
console.log('BufferLoader: XHR error');
}
request.send();
}
BufferLoader.prototype.load = function() {
for (var i = 0; i < this.urlList.length; ++i)
this.loadBuffer(this.urlList[i], i);
}
我不明白。我播放音频很好。我的调试器中没有出现任何错误,只是停止工作。我也用其他应用程序测试了我的 chromecast。他们都通过音频。有人发现这门课有什么问题吗?
我什至使用调试器逐步检查它,检查所有变量,因为我似乎有数据。 start() 方法什么也不做。
【问题讨论】:
-
好的,我使用 crhome 浏览器对此进行了更多测试,效果很好。我在这里缺少音量控制吗?我看到了带有投射界面的android上的那个。那是一直出现的。 WEB API 本身的音量控制有什么我遗漏的吗?
-
只是记录我所做的事情。我在播放方法中添加了一些增益控制。它改变了 chrome 中的音量,但在 chromecast 中仍然没有改变。 //创建增益控制 var gainNode = this.context.createGain(); // 将源连接到增益节点。 this.sources[index].connect(gainNode); // 将增益节点连接到目的地。 gainNode.connect(this.context.destination); gainNode.gain.value = 4.0; this.sources[index].start(0);//播放然后重新加载缓冲区以减少用户操作和声音播放之间的延迟
标签: android audio chromecast jscript