-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24e23ee
commit d13de49
Showing
1 changed file
with
27 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,37 @@ | ||
const RADIO_URL = 'https://64.150.176.42:8242/;stream.mp3'; | ||
|
||
class MediaPlayer { | ||
private audio = new Audio(); | ||
#audio = new Audio(); // Private class field for encapsulation | ||
|
||
constructor(private url: string) { | ||
chrome.browserAction.setBadgeText({ text: this.getBadgeText() }); | ||
chrome.browserAction.onClicked.addListener(() => this.toggle()); | ||
this.#updateBadge(); | ||
chrome.browserAction.onClicked.addListener(() => this.#toggle()); | ||
} | ||
|
||
private getBadgeText(): string { | ||
return this.audio.paused ? 'OFF' : 'ON'; | ||
} | ||
|
||
private updateBadge(): void { | ||
chrome.browserAction.setBadgeText({ text: this.getBadgeText() }); | ||
} | ||
|
||
toggle(): void { | ||
this.audio.paused ? this.play() : this.stop(); | ||
this.updateBadge(); | ||
} | ||
|
||
private stop(): void { | ||
this.audio.pause(); | ||
this.audio.src = ''; | ||
} | ||
|
||
private play(): void { | ||
this.audio.src = this.url; | ||
this.audio | ||
.play() | ||
.then(() => console.log('Playback started')) | ||
.catch(console.error); | ||
} | ||
#getBadgeText = () => (this.#audio.paused ? 'OFF' : 'ON'); // Arrow function for concise logic | ||
|
||
#updateBadge = () => | ||
chrome.browserAction.setBadgeText({ text: this.#getBadgeText() }); // Inline badge update | ||
|
||
#toggle = () => { | ||
this.#audio.paused ? this.#play() : this.#stop(); | ||
this.#updateBadge(); | ||
}; | ||
|
||
#stop = () => { | ||
this.#audio.pause(); | ||
this.#audio.src = ''; | ||
}; | ||
|
||
#play = async () => { | ||
this.#audio.src = this.url; | ||
try { | ||
await this.#audio.play(); | ||
console.log('Playback started'); // Improved logging | ||
} catch (error) { | ||
console.error('Playback failed:', error); // Improved error handling | ||
} | ||
}; | ||
} | ||
|
||
export default new MediaPlayer(RADIO_URL); |