Skip to content

Commit

Permalink
Load the stream on play, add source for non-hls stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
kovipu committed Apr 20, 2024
1 parent bddc3b7 commit 1edf9a4
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { ChatWrapper } from '@/components/ShoutBox/shoutbox';
import VideoPlayer from '@/components/videoPlayer';
import { ShoutBoxAndVideoProvider } from '@/hooks/useShoutboxAndVideo';

const isHlsLive = process.env.NEXT_PUBLIC_HLS_MODE === 'live';
const isHlsLive =
Hls.isSupported() && process.env.NEXT_PUBLIC_HLS_MODE === 'live';

const AUDIO_STREAM_URL = 'https://player.turunwappuradio.com/wappuradio.mp3';
const HLS_STREAM_URL = 'https://stream.turunwappuradio.com/twr_chunklist.m3u8';
Expand All @@ -22,7 +23,7 @@ const MyApp = ({ Component, pageProps }: AppProps) => {
const [playClicked, setPlayClicked] = useState(false);

const loadAudioStream = useCallback(() => {
if (Hls.isSupported() && isHlsLive) {
if (isHlsLive) {
hls.current = new Hls();
hls.current.loadSource(HLS_STREAM_URL);
hls.current.attachMedia(audioEl.current);
Expand All @@ -41,11 +42,11 @@ const MyApp = ({ Component, pageProps }: AppProps) => {
const handlePlayPause = () => {
setPlayClicked(true);

if (audioEl.current.paused) audioEl.current.play();
else {
// Pause, but then load the stream again ready to start
audioEl.current.pause();
if (audioEl.current.paused) {
loadAudioStream();
audioEl.current.play();
} else {
audioEl.current.pause();
}
setPlaying(!playing);
};
Expand All @@ -63,7 +64,9 @@ const MyApp = ({ Component, pageProps }: AppProps) => {

return (
<ShoutBoxAndVideoProvider>
<audio ref={audioEl}></audio>
<audio ref={audioEl}>
{isHlsLive ? null : <source src={AUDIO_STREAM_URL} tyoe="audio/mpeg" />}
</audio>
<Component
{...pageProps}
playing={playing}
Expand Down

0 comments on commit 1edf9a4

Please sign in to comment.