07-24-2023, 07:28 AM
Ok so i had this class working and playing sound. It suddenly stopped playing sound. If I debug it in the chromecast debugger all the data members get fill when I create an istance of it and call the init() function.
var audioplayer = new cast.AudioPlayer();
audioplayer.init();
Then after I am sure the .wav files are loaded, I call my play method
audioplayer.play(cast.AudioPlayer.welcome);//pass index of buffer
Here is the class
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;
})();
And here is the buffer loader, sorry about the two different types of encapsulation.
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);
}
I don't get it. I was playing audio fine. No errors are showing up in my debugger, just stopped working. I tested my chromecast with other apps as well. They all pass audio. Does anyone see any issues with this class?
I even step through it with the debugger, inspecting all variables as i go seem to have the data. The start() method just does not do anything.
var audioplayer = new cast.AudioPlayer();
audioplayer.init();
Then after I am sure the .wav files are loaded, I call my play method
audioplayer.play(cast.AudioPlayer.welcome);//pass index of buffer
Here is the class
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;
})();
And here is the buffer loader, sorry about the two different types of encapsulation.
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);
}
I don't get it. I was playing audio fine. No errors are showing up in my debugger, just stopped working. I tested my chromecast with other apps as well. They all pass audio. Does anyone see any issues with this class?
I even step through it with the debugger, inspecting all variables as i go seem to have the data. The start() method just does not do anything.