Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to play from custom button #244

Closed
jonalxh opened this issue Sep 18, 2024 · 2 comments
Closed

How to play from custom button #244

jonalxh opened this issue Sep 18, 2024 · 2 comments

Comments

@jonalxh
Copy link

jonalxh commented Sep 18, 2024

I have a custom button and I want to be able to make this audio player play when I click on it.

My current React code looks like this:

const audioPlayerRef = useRef(null);

const onPlayClick = () => {
    // TODO: Make the player play from here
    audioPlayerRef.play();
}

return (
    <Button
		className='rounded-full bg-gray-600 w-[40px] h-[40px] p-0' title='Play audio'
		onClick={() => onPlayClick()}
	>
		<Play size={24} />
    </Button>

    <AudioPlayer
	ref={audioPlayerRef}
	src={transcript.file_url}
	autoPlayAfterSrcChange={false}
	customAdditionalControls={[]}
	layout='stacked-reverse'
	className='custom-audio-player'
    ></AudioPlayer>
)

How can I make it play from my custom button? I appreciate all your help.

@stepannemejc
Copy link

stepannemejc commented Nov 10, 2024

You can start/pause playback using this code:

const playerRef = useRef<AudioPlayer>(null);
const [isPlaying, setIsPlaying] = useState<boolean>(false);

const handlePlayPause = () => {
	if (isPlaying) {
		playerRef?.current?.audio?.current?.pause();
	} else {
		playerRef?.current?.audio?.current?.play();
	}
};

To determine status of player, use onPlay and onPause to set isPlaying state

@jonalxh
Copy link
Author

jonalxh commented Nov 13, 2024

Thanks a ton! @stepannemejc

@lhz516 lhz516 closed this as completed Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants