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

Button Play doesn't work #169

Closed
krono999 opened this issue Sep 14, 2022 · 11 comments
Closed

Button Play doesn't work #169

krono999 opened this issue Sep 14, 2022 · 11 comments

Comments

@krono999
Copy link

krono999 commented Sep 14, 2022

hello, im using the audio player, the button next and previous work well but, when i open at the first time or reloaded the web, the button play doesn't load and play the first song, only works if i clicke previous or next.
other doubt: i test the code in stackblitz with the code example and it works! and is the same i use in this example, but in my localhost doesnt work
please help me :(

here is my code:

import { useState } from "react";
import AudioPlayer from "react-h5-audio-player";
import "react-h5-audio-player/lib/styles.css";

export default function AudioPlayerComponent() {
const musicTracks = [
{
name: "song1",
src: "https://res.cloudinary.com/mp3", // only for the example
},
{
name: "song2",
src: "https://res.cloudinary.com/mp3", // only for the example
},
];

const [trackIndex, setTrackIndex] = useState(0);

const handleClickPrevious = () => {
setTrackIndex((currentTrack) =>
currentTrack === 0 ? musicTracks.length - 1 : currentTrack - 1
);
};

const handleClickNext = () => {
setTrackIndex((currentTrack) =>
currentTrack < musicTracks.length - 1 ? currentTrack + 1 : 0
);
};

return (


<AudioPlayer
style={{
backgroundColor: "transparent",
color: "purple",
fontFamily: "monospace",
marginTop: "-70px",
fontSize: "15px",
}}
src={musicTracks[trackIndex].src}
onPlay={musicTracks[trackIndex].src}
showSkipControls={true}
showJumpControls={false}
header={Now playing: ${musicTracks[trackIndex].name}}
onClickPrevious={handleClickPrevious}
onClickNext={handleClickNext}
onEnded={handleClickNext}
/>

);
}

@lhz516
Copy link
Owner

lhz516 commented Sep 17, 2022

Please fork the codepen example in README to reproduce this error, then I can take a look.

@BrianEmilius
Copy link

I have the same issue - it looks like src isn't loaded properly during first render in dev-mode.

Here's an example of it not working:
https://github.com/BrianEmilius/react-player-example/blob/main/src/App.js

@BrianEmilius
Copy link

I should add that it works in production (after build), just not in development mode (npm start)

@totti-rdz
Copy link

totti-rdz commented Sep 19, 2022

Interesting, while investigating another problem I noticed that on localhost I can not play the audio file.

I also added an onPlay function, that just logs the clicking of the button, but apparently it does not even trigger, so the button is not clicked at all.

And yes, in production it works fine (see vercel link)

https://react-h5-audio-player.vercel.app/
https://github.com/totti-rdz/react-h5-audio-player

One last thing, I use the audio player in another project for some months now and this never seem to be an issue. But I'm quiet sure, it is the same version, but there have been some commits inbetween, but only to the README I think

@krono999
Copy link
Author

krono999 commented Sep 20, 2022

i dont have codepen or sandbox account, the issue is like i said, if i press button play when you are developing the app in your local doesnt work, but the other two button works well. i put the code example.
i saw other example like stack blitz, and put my code in that web examples and my code works! xD I dont know what is the problem honstely

@krono999
Copy link
Author

i see the code of brian and is bit similar of mine and he had the same problem

@ferdiamg
Copy link

ferdiamg commented Sep 22, 2022

This package has the same problem as all other not updated packages right now.
Due to React 18 running useEffect twice with reactStrictMode enabled, something in this package got broken. Disable the strictMode and it'll run just fine. However its not recommended to just switch off the strictMode for a npm package.

const nextConfig = {
  reactStrictMode: false,
};

Alright, I got it fixed:
grafik
Here you can see how with React 18 react-h5-audio-player gets mounted, then unmounted, then mounted again. However after the second mount the source is gone. Thats why onPlay() nothing happens. In the componentWillUnmount() we get the ref and remove its source attribute. Removing that one line will make the src attribute persist since the ref is there on the next mount and the package is working as wanted again :)

Here is the PR: #179

@lhz516
Copy link
Owner

lhz516 commented Sep 23, 2022

The original idea of removing src attribute while unmount is to stop the audio from playing when the player is removed from the DOM. If we remove this line, we need some similar features.

I'm considering using audio.pause() at the unmount lifecycle. Any opinions?

Note: The good news is that this issue only affects dev build, and it works fine in production build.

@totti-rdz
Copy link

Interesting, that the audio player would keep playing after the component gets unmounted. Do you have more info on that?

I think replacing the function to remove src with audio.pause() function is a valid solution.

@lhz516
Copy link
Owner

lhz516 commented Sep 24, 2022

Oh never mind, I just tested it and found that the audio won't stop when the component is destroyed. Let me just remove this line

@lhz516
Copy link
Owner

lhz516 commented Sep 24, 2022

Published 3.8.6

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

5 participants