I'm using npm react-player to play some audio and video files In a react app. I have allowed the user to toggle an autoplay feature for audio.
const ReactPlayer = require('react-player').default;
const autoplay = true; // if user chose to enable autoplay
React.createElement(ReactPlayer, { playing: autoplay, url: 'some/audio.wav' })
When the player is rendered and starts playing in the chrome android browser, it also starts a native android player, (tho one accessible in the os notification area)
The two players are in perfectly sync, but if the browser player is paused the native one continues. Also if dragging dot on the timeline on the browser player they become out of sync and talk over each other.
Funny thing is that when autoplay is not tuned on, but the user have to manually press a button that sets the playing prop to true
, the native audio player and the browser players controls affect each other, if pausing in the browser both pauses, if pausing in the notification area both pause and stays in sync.
How can I fix this?
Update:
I have not tried a bit more around. Instead of setting playing
to true before the component in rendered I have tried to get the html element in componentDidMount()
and use the play()
function there. Unfortunately this has the same behaviour it seems to start 2 separate players that eventually will become out of sync :(
componentDidMount () {
const player = document.getElementById(this.state.id);
if (player) {
const audio = player.getElementsByTagName('audio');
if (audio && audio.length > 0) {
this.state.autoplay && player.firstElementChild.play();
}
}
},