Comments (8)
Okay, thx a lot!
from useaudioplayer.
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.
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.
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.
@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.
I'm sorry that my description is not very clear. I created a example on codesandbox.io. Thx for your work!
from useaudioplayer.
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.
@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)
- Case insensitive formats HOT 2
- usePosition doesnt work anymore HOT 3
- starting with empty src loads infinitely HOT 2
- Download audio HOT 1
- Support SSR HOT 6
- `AudioLoadOptions` shape does not correspond Howler constructor options and missing some of them HOT 9
- isLoading boolean HOT 4
- HTML5 Audio pool exhausted, returning potentially locked audio object. Is this because of this library or not? HOT 1
- Adding `player` back — Howler Instance Reference HOT 4
- Error in the Readme, fade arg says second not ms HOT 2
- Finish playing before destroying the sound currently playing HOT 2
- Inconsistencies on lock screen controls when playing in background on iOS HOT 2
- Audio not changing when recording is different but link is same HOT 4
- useGlobalAudioPlayer and useAudioPlayer seems to mix up their states HOT 4
- How to adjust the audio playback progress ? HOT 1
- Upgrade Howler
- Refactor state management to decouple from Howler
- Refactor to use React.useSyncExternalStore
- playback speed sounds like a chipmunk HOT 1
- Audio playback stops when safari unfocused on ios HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from useaudioplayer.