-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(p-radio): Use icecast-metadata-player to play audio, update UI, …
…added a bunch of other radio source
- Loading branch information
Showing
7 changed files
with
358 additions
and
112 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { perceptualToAmplitude } from "@discordapp/perceptual"; | ||
import IcecastMetadataPlayer from "icecast-metadata-player"; | ||
import { useEffect, useState } from "react"; | ||
import { useDisposable } from "use-disposable"; | ||
|
||
import { usePluginAPI } from "../pluginApi"; | ||
|
||
const Player = () => { | ||
const pluginApi = usePluginAPI(); | ||
|
||
const isPlaying = pluginApi.renderer.useData((x) => x.isPlaying); | ||
const volume = pluginApi.renderer.useData((x) => x.volume); | ||
const url = pluginApi.renderer.useData((x) => x.url!); | ||
|
||
const [localIsPlaying, setLocalIsPlaying] = useState(false); | ||
|
||
const player = useDisposable(() => { | ||
const player = new IcecastMetadataPlayer(url!, { | ||
onPlay: () => { | ||
setLocalIsPlaying(true); | ||
}, | ||
onStop: () => { | ||
setLocalIsPlaying(false); | ||
}, | ||
metadataTypes: ["ogg"], | ||
}); | ||
return [ | ||
player, | ||
() => { | ||
player.detachAudioElement(); | ||
}, | ||
]; | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (isPlaying && !localIsPlaying) { | ||
player?.play(); | ||
} else if (!isPlaying && localIsPlaying) { | ||
player?.stop(); | ||
} | ||
}, [isPlaying, localIsPlaying, player]); | ||
|
||
useEffect(() => { | ||
if (player) { | ||
player.audioElement.volume = perceptualToAmplitude( | ||
Math.min(Math.max(0, volume ?? 1), 1), | ||
); | ||
} | ||
}, [player, volume]); | ||
|
||
return null; | ||
}; | ||
|
||
export default Player; |
Oops, something went wrong.