Player stops with Next.js Router page change
See original GitHub issueHello,
I’m building a website that features one player at the bottom of the page (kind of like SoundCloud and MixCloud) that will allow the user to select through songs on different pages and will play the track. I’ve always used Plyr since it’s ease of use and customization.
I’ve set up one main component with Plyr that has one track for testing purposes. Code below:
const audioSrc = {
type: 'audio',
sources: [
{
src: '/audio/bad vibes forever.flac'
}
]
}
export default function PlyrPlayer() {
return (
<div className="fixed left-0 bottom-0 w-full">
<Plyr
source={audioSrc}
options={{
keyboard: {
global: true
},
debug: true,
volume: 0.5,
controls: [
'play-large', // The large play button in the center
'restart', // Restart playback
'play', // Play/pause playback
'fast-forward', // Fast forward by the seek time (default 10 seconds)
'progress', // The progress bar and scrubber for playback and buffering
'current-time', // The current time of playback
'duration', // The full duration of the media
'mute', // Toggle mute
'volume', // Volume control
'captions', // Toggle captions
'pip', // Picture-in-picture (currently Safari only)
'airplay' // Airplay (currently Safari only)
// 'download' // Show a download button with a link to either the current source or a custom URL you specify in your options
],
tooltips: { controls: true, seek: true },
storage: { enabled: true, key: 'plyr' }
}}
/>
</div>
)
}
And then I import the component as follows in my Layout so it renders on every page. The expected behavior would be to keep playing on every page. It was working that way when I used react-player.
However, on every page change, the player stops and restarts. I use Next Router to handle page changes.
I enabled debugging and noticed every time I load a new page, this appears in the console:
Cancelled network requests
Not sure if I am missing something minor or if this is bug with nextJS routing, but I would highly appreciate any advice on how to prevent this and keep it playing. Soon I’ll add a way to select tracks and have timestamps + Firebase, etc.
Thank you!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top GitHub Comments
That is good to hear, and we had planned to complete the doc, but we didn’t get the time for updating the current doc so much. Anyway as I mentioned the road map for new API usage has been started, and it will be backward compatibly with extra features, and we try our best to make it crystal clear. And as always any help is appreciated.
IMO the best way to use the plyr-react is conditional, in a case you need something like
fullscreen
, changingseek
,volume
or … with user interaction esp via click (and probably from another component) it is better to use our imperative API can be grabbed via theuseRef
andAPITypes
. A basic example can be found here in a codesandbox. On the other hand for interaction that needs the player to be initialized, Plyr doesn’t rely on react update mechanism itself. currently, it is dependent onsource
andoptions
props, but you can connect it to any other thing if you want by simply passing thePlyr
your custom key.I am afraid I didn’t understand your question well but rendering from all over the place can be completely done duo to our imperative API (
ref.current.plyr
is complete Plyr instance).React Context
for handling all API interactions, and feel free to open a new issue if you found something unpleasant or buggy.Finally, we are happy to hear that Plyr-react was useful for you, and that’s all open-source communication means.
Happy to hear it solves your issue, for the audio type, there are two things you must do.
"audio/wav"
Here is a complete codesnadbox
Out of the topic:
The song was nice 👍🏻 🤗 In a case, you are want to hear some other thing from different cultures and languages ☘️, check this out: I Promise you
And Happy new year @erubescent