Giter Site home page Giter Site logo

Comments (8)

Vang-z avatar Vang-z commented on May 20, 2024 1

Okay, thx a lot!

from useaudioplayer.

Vang-z avatar Vang-z commented on May 20, 2024 1

I'm sorry for not replying to you in time, because I was ill during this period. I will test these methods as soon as possible and hope they work as scheduled. Thank you!

from useaudioplayer.

E-Kuerschner avatar E-Kuerschner commented on May 20, 2024

hi @Vang-z thank you for opening this. Can you please provide the following?

  • library version
  • browser/platform
  • a snippet of the code you are trying

I will need to reliably reproduce your issue before I can start debugging this. Hopefully I will be able to address over the weekend.

from useaudioplayer.

Vang-z avatar Vang-z commented on May 20, 2024

library version: 1.2.4
browser/platform: chrome 96.0.4664.110
a snippet of the code:

// Initial Code
const PlayerCore: React.FC<{ song: any }> = ({song}) => {
  const classes = playerCoreStyles();

  const dispatch = useDispatch();
  const player = useSelector(state => state.musicPlayer);

  const {togglePlayPause, play, pause, volume, playing} = useAudioPlayer({
    src: song.src,
    autoplay: player.autoplay,
    volume: player.volume / 100,
    html5: true,
    "onend": () => {
      dispatch(musicPlayerSlice.actions.dispatchAutoplay(true));
      dispatch(musicPlayerSlice.actions.dispatchEnd(true));
    }
  });
  // render code
}
// because of the useAudioPlayer function don't accept the audio scr array
// I have a bad solution, It will to change the src of the useAudioPlayer, when the player was ending.
  useEffect(() => {
    if (player.autoplay && player.end) {
      dispatch(musicPlayerSlice.actions.dispatchSrc(newSrc);
      dispatch(musicPlayerSlice.actions.dispatchEnd(false));
    }
  }, [dispatch, togglePlayPause, player, playing])

But there are still problems with this, if the newSrc equal to the old src, we must use play() function to play the audio. By the way useAudioPlayer is a excellent lib for react :)

from useaudioplayer.

E-Kuerschner avatar E-Kuerschner commented on May 20, 2024

@Vang-z i can see one issue with your useEffect. If I'm understanding what you want to do correctly, you want to begin the necxt sound when the previous one ends. However, based on your second code snippet I don't think that is what is happening. If you want to trigger some behavior at the end of a sound you can use the onend callback in the options you pass to the main hook.

Please try putting your "play next track" logic in there and not in this effect first. Please let me know if I misunderstood your issue.

from useaudioplayer.

Vang-z avatar Vang-z commented on May 20, 2024

I'm sorry that my description is not very clear. I created a example on codesandbox.io. Thx for your work!

from useaudioplayer.

E-Kuerschner avatar E-Kuerschner commented on May 20, 2024

Thanks for setting that sandbox up. I will investigate deeper tomorrow. In the meantime perhaps try using the load method from useAudioPlayer. That may be the only way to support loading subsequent sounds for now.

from useaudioplayer.

E-Kuerschner avatar E-Kuerschner commented on May 20, 2024

@Vang-z - good news! I was able to achieve autoplaying the next song without putting up a new release. Please review the code below and let me know if it doesn't work for you. Would you mind also advising how the documentation can be improved such that someone such as yourself could implement something like this on their own? Also feel free to push up a PR yourself if you would like to contribute to the docs

const songs = [sound1, sound2];
export function AutoPlayNextSound() {
    const [songIndex, setSongIndex] = useState(0);

    const { togglePlayPause, ready, playing } = useAudioPlayer({
        src: songs[songIndex],
        html5: true,
        autoplay: true,
        onend: () => {
            setSongIndex(index => index + 1);
        }
    });

    const { position, duration, seek } = useAudioPosition({
        highRefreshRate: true
    });

    if (!ready) return <h1>audio {songs[songIndex]} is loading</h1>
    return (
        <>
            <div>Currently playing: {songs[songIndex]}</div>
            <button onClick={togglePlayPause}>
                {playing ? "pause" : "play"}
            </button>
            <button onClick={() => seek(duration * 0.99)}>skip to end</button>
            <div>
                {position.toFixed(2)} / {duration.toFixed(2)}
            </div>
        </>
    );
}

export default AutoPlayNextSound;

The key bits here are setting the autoplay property and to not store the audio objects themselves in state, but rather a pointer to them.

from useaudioplayer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.