switch to HLS streams for desktop as well
This commit is contained in:
@@ -1,20 +1,18 @@
|
||||
import { useState, useEffect, useRef } from "react";
|
||||
import { Howl, Howler } from "howler";
|
||||
import Hls from "hls.js";
|
||||
import { metaData } from "../config";
|
||||
import Play from "@mui/icons-material/PlayArrow";
|
||||
import Pause from "@mui/icons-material/Pause";
|
||||
import "@styles/player.css";
|
||||
|
||||
const API_URL = "https://api.codey.lol";
|
||||
Howler.html5PoolSize = 32;
|
||||
const isIOS = /iP(ad|hone|od)/.test(navigator.userAgent);
|
||||
const STATIONS = {
|
||||
main: { label: "Main", streamPath: "/sfm.ogg" },
|
||||
rock: { label: "Rock", streamPath: "/rock.ogg" },
|
||||
rap: { label: "Rap", streamPath: "/rap.ogg" },
|
||||
electronic: { label: "Electronic", streamPath: "/electronic.ogg" },
|
||||
classical: { label: "Classical", streamPath: "/classical.ogg" },
|
||||
pop: { label: "Pop", streamPath: "/pop.ogg" },
|
||||
main: { label: "Main" },
|
||||
rock: { label: "Rock" },
|
||||
rap: { label: "Rap" },
|
||||
electronic: { label: "Electronic" },
|
||||
classical: { label: "Classical" },
|
||||
pop: { label: "Pop" },
|
||||
};
|
||||
|
||||
let activeInterval = null;
|
||||
@@ -39,7 +37,8 @@ export function Player() {
|
||||
const [elapsed, setElapsed] = useState(0);
|
||||
const [duration, setDuration] = useState(0);
|
||||
|
||||
const soundRef = useRef(null);
|
||||
const audioRef = useRef(null);
|
||||
const hlsRef = useRef(null);
|
||||
const uuidRef = useRef(null);
|
||||
const lastStationData = useRef(null);
|
||||
|
||||
@@ -52,77 +51,67 @@ export function Player() {
|
||||
const progress = duration > 0 ? (elapsed / duration) * 100 : 0;
|
||||
|
||||
const playStream = () => {
|
||||
let streamPath = STATIONS[activeStation].streamPath;
|
||||
if (isIOS) {
|
||||
let lcStation = activeStation.toLowerCase();
|
||||
streamPath = `/hls/${lcStation}/${lcStation}.m3u8`;
|
||||
console.log(`Replaced streamPath: ${streamPath}`);
|
||||
}
|
||||
const streamUrl =
|
||||
"https://stream.codey.lol:9992" +
|
||||
streamPath +
|
||||
`?t=${Date.now()}`;
|
||||
const lcStation = activeStation.toLowerCase();
|
||||
const streamUrl = `https://stream.codey.lol/hls/${lcStation}/${lcStation}.m3u8?t=${Date.now()}`;
|
||||
|
||||
if (soundRef.current) {
|
||||
soundRef.current.stop();
|
||||
soundRef.current.unload();
|
||||
if (hlsRef.current) {
|
||||
hlsRef.current.destroy();
|
||||
hlsRef.current = null;
|
||||
}
|
||||
|
||||
const newHowl = new Howl({
|
||||
src: [streamUrl],
|
||||
html5: true,
|
||||
onend: () => newHowl.play(),
|
||||
onplay: () => setIsPlaying(true),
|
||||
onpause: () => setIsPlaying(false),
|
||||
onstop: () => setIsPlaying(false),
|
||||
onloaderror: (_, err) => console.error("Load error", err),
|
||||
onplayerror: (_, err) => {
|
||||
console.error("Play error", err);
|
||||
setTimeout(() => newHowl.play(), 1000);
|
||||
},
|
||||
if (audioRef.current.canPlayType("application/vnd.apple.mpegurl")) {
|
||||
// Native HLS support (Safari)
|
||||
audioRef.current.src = streamUrl;
|
||||
} else if (Hls.isSupported()) {
|
||||
const hls = new Hls({
|
||||
maxBufferLength: 60,
|
||||
});
|
||||
hls.loadSource(streamUrl);
|
||||
hls.attachMedia(audioRef.current);
|
||||
hlsRef.current = hls;
|
||||
} else {
|
||||
console.error("HLS is not supported in this browser.");
|
||||
}
|
||||
|
||||
soundRef.current = newHowl;
|
||||
newHowl.play();
|
||||
audioRef.current.play().then(() => setIsPlaying(true)).catch((e) => {
|
||||
console.error("Playback failed:", e);
|
||||
});
|
||||
};
|
||||
|
||||
const togglePlayback = () => {
|
||||
if (!audioRef.current) return;
|
||||
|
||||
if (isPlaying) {
|
||||
soundRef.current?.pause();
|
||||
audioRef.current.pause();
|
||||
setIsPlaying(false);
|
||||
} else {
|
||||
if (!soundRef.current) {
|
||||
playStream();
|
||||
} else {
|
||||
soundRef.current.play();
|
||||
}
|
||||
setIsPlaying(true);
|
||||
audioRef.current.play().then(() => setIsPlaying(true));
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (soundRef.current) {
|
||||
soundRef.current.stop();
|
||||
soundRef.current.unload();
|
||||
soundRef.current = null;
|
||||
if (audioRef.current) {
|
||||
audioRef.current.pause();
|
||||
audioRef.current.src = "";
|
||||
setIsPlaying(false);
|
||||
}
|
||||
|
||||
if (isPlaying) {
|
||||
playStream();
|
||||
|
||||
return () => {
|
||||
if (hlsRef.current) {
|
||||
hlsRef.current.destroy();
|
||||
}
|
||||
|
||||
};
|
||||
}, [activeStation]);
|
||||
|
||||
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
clearGlobalMetadataInterval();
|
||||
currentStationForInterval = activeStation;
|
||||
|
||||
const setPageTitle = (artist, song) => {
|
||||
document.title = metaData.title + ` - Radio - ${artist} - ${song} [${activeStation}]`;
|
||||
}
|
||||
document.title = `${metaData.title} - Radio - ${artist} - ${song} [${activeStation}]`;
|
||||
};
|
||||
|
||||
const fetchTrackData = async () => {
|
||||
try {
|
||||
@@ -173,9 +162,7 @@ useEffect(() => {
|
||||
fetchTrackData();
|
||||
activeInterval = setInterval(fetchTrackData, 1000);
|
||||
|
||||
return () => {
|
||||
clearGlobalMetadataInterval();
|
||||
};
|
||||
return () => clearGlobalMetadataInterval();
|
||||
}, [activeStation]);
|
||||
|
||||
const remaining = duration - elapsed;
|
||||
@@ -187,8 +174,6 @@ const progressColorClass =
|
||||
? "bg-yellow-400 dark:bg-yellow-300"
|
||||
: "bg-blue-500 dark:bg-blue-400";
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="station-tabs flex gap-2 justify-center mb-4 flex-wrap z-10 relative">
|
||||
@@ -211,27 +196,35 @@ const progressColorClass =
|
||||
<div className="c-containter">
|
||||
<div className="music-container mt-8">
|
||||
<section className="album-cover">
|
||||
<div class="music-player__album" title="Album">{trackAlbum}</div>
|
||||
<img src={coverArt}
|
||||
<div className="music-player__album" title="Album">
|
||||
{trackAlbum}
|
||||
</div>
|
||||
<img
|
||||
src={coverArt}
|
||||
className="cover"
|
||||
title={trackAlbum ? `"${trackAlbum}" Cover Art` : "Cover Art"}
|
||||
alt={trackAlbum ? `"${trackAlbum}" Cover Art` : "Cover Art"} />
|
||||
alt={trackAlbum ? `"${trackAlbum}" Cover Art` : "Cover Art"}
|
||||
/>
|
||||
</section>
|
||||
|
||||
<section className="music-player">
|
||||
<h1 className="music-player__header">serious.FM</h1>
|
||||
<h1 className="music-player__title">{trackTitle}</h1>
|
||||
<h2 className="music-player__author">{trackArtist}</h2>
|
||||
{trackGenre && <h2 className="music-player__genre">{trackGenre}</h2>}
|
||||
|
||||
<div className="music-time">
|
||||
<p className="music-time__current">{formatTime(elapsed)}</p>
|
||||
<p className="music-time__last">{formatTime(duration - elapsed)}</p>
|
||||
</div>
|
||||
|
||||
<div className="w-full h-2 rounded bg-neutral-300 dark:bg-neutral-700 overflow-hidden">
|
||||
<div
|
||||
className={`h-full transition-all duration-200 ${progressColorClass}`}
|
||||
style={{ width: `${progress}%` }}
|
||||
></div>
|
||||
</div>
|
||||
|
||||
<div className="music-control">
|
||||
<div
|
||||
className="music-control__play"
|
||||
@@ -246,6 +239,8 @@ const progressColorClass =
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<audio ref={audioRef} preload="none" />
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
|
Reference in New Issue
Block a user