74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
const EventEmitter = require('events');
|
|
|
|
class LocalRadioPlayer extends EventEmitter {
|
|
constructor() {
|
|
super();
|
|
// These are example streams. You will need to set up your own
|
|
// Icecast/HTTP streams and replace these URLs.
|
|
this._stations = {
|
|
'My Station': 'http://192.168.1.5:6767/Ninja%20Tuna.mp3'
|
|
};
|
|
this._audio = null;
|
|
console.log("LocalRadioPlayer initialized");
|
|
}
|
|
|
|
getStations(options) {
|
|
console.log("LocalRadioPlayer: getStations called");
|
|
const stationList = Object.keys(this._stations).map(genre => ({
|
|
name: genre,
|
|
id: genre
|
|
}));
|
|
return Promise.resolve(stationList);
|
|
}
|
|
|
|
play(stationData) {
|
|
console.log(`LocalRadioPlayer: play called with`, stationData);
|
|
if (this._audio) {
|
|
this._audio.pause();
|
|
}
|
|
const streamUrl = this._stations[stationData.id];
|
|
if (!streamUrl) {
|
|
const err = new Error(`Station ${stationData.id} not found.`);
|
|
console.error("LocalRadioPlayer Error:", err);
|
|
this.emit('error', err);
|
|
return;
|
|
}
|
|
|
|
console.log(`LocalRadioPlayer: Playing stream from ${streamUrl}`);
|
|
this._audio = new Audio(streamUrl);
|
|
this._audio.addEventListener('error', (e) => {
|
|
console.error('LocalRadioPlayer Audio Error:', e);
|
|
this.emit('error', new Error('Audio playback failed.'));
|
|
});
|
|
|
|
this._audio.play()
|
|
.then(() => {
|
|
console.log("LocalRadioPlayer: Playback started.");
|
|
this.emit('song-data', {
|
|
title: `Streaming ${stationData.id}`,
|
|
artist: 'Local Radio',
|
|
albumArt: '' // No artwork for local streams
|
|
});
|
|
})
|
|
.catch(err => {
|
|
console.error("LocalRadioPlayer Playback Error:", err);
|
|
this.emit('error', err);
|
|
});
|
|
}
|
|
|
|
stop() {
|
|
console.log("LocalRadioPlayer: stop called");
|
|
if (this._audio) {
|
|
this._audio.pause();
|
|
this._audio = null;
|
|
}
|
|
}
|
|
|
|
resizeArtwork(options) {
|
|
// Not applicable for local streaming
|
|
return Promise.resolve('');
|
|
}
|
|
}
|
|
|
|
module.exports = LocalRadioPlayer;
|