Skip to content

Commit

Permalink
Fix safari play on select
Browse files Browse the repository at this point in the history
  • Loading branch information
jalvarado91 committed Dec 27, 2023
1 parent 1e814e5 commit d957823
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/client/components/AudioPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef } from "react";
import React, { useEffect, useRef, useState } from "react";

export interface AudioPlayerProps {
mp3StreamUrl: string | null;
Expand All @@ -10,6 +10,7 @@ export interface AudioPlayerProps {
onPlay: () => void;
cuePosition?: number;
}

export function AudioPlayer({
mp3StreamUrl,
onReady,
Expand All @@ -21,17 +22,18 @@ export function AudioPlayer({
cuePosition,
}: AudioPlayerProps) {
const ref = useRef<HTMLAudioElement>(null);
const [shouldPlay, setShouldPlay] = useState(false);

async function onCanPlayThrough() {
const durationSecs = ref.current?.duration ?? 0;
const durationMillis = durationSecs * 1000;

onReady(durationMillis);

if (playing) {
if (shouldPlay) {
ref.current
?.play()
.then()
.then(() => setShouldPlay(false))
.catch((err) => console.error(`onCanPlay ${err}`));
}
}
Expand All @@ -40,6 +42,7 @@ export function AudioPlayer({
if (ref.current && mp3StreamUrl) {
ref.current.src = mp3StreamUrl;
ref.current.load();
setShouldPlay(true);
}

return () => {
Expand Down

1 comment on commit d957823

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for soulector ready!

✅ Preview
https://soulector-lgb7qwkrb-jalvarado91.vercel.app

Built with commit d957823.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.