SetMusic() forces AutoPlay to true
See original GitHub issueDescription
Calling setMusic
on a paused player will automatically play the music, even with autoplay=false
.
The Steps to Reproduce
- Load music into APlayer instance.
- Play music.
- Stop music.
- Change music loaded into APlayer (e.g.
ap.option.music[0] = {...}
). - Call
setMusic()
(e.g.ap.setMusic(0)
) to update the appearance of the player.
Expected Behavior: The player should reload the information (including metadata, if autoplay="metadata"
was set) but should not begin playback.
Actual Behavior: The player automatically begins playback.
Demo of the Problem
Please see this JSFiddle for an example. Do the following to see it:
- Press the Play button.
- Press the Pause button.
- Press the custom Swap button.
I don’t know why but the audio in the JSFiddle also won’t change. All other information properly changes but the loaded music does not update. Perhaps this is another bug?
Version Information
APlayer: 1.6.0 Browsers: All
Extra Information
This is likely caused by setting this.option.autoplay = true;
at the end of the setMusic
function. At the very least, that line should be guarded by a check for playlist, rather than a single loaded audio file. Another option would be to check the mode
option for circulation
or order
.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
I’m working on a fix for this right now, actually, and will issue a PR soon.
The key insight is that
autoplay
is being used for multiple purposes:Whoops. Changing one affects the other! Rather,
autoplay
should only be checked during initialization (the constructor). Then, the second situation I outlined above can be handled in the code that callssetMusic
, here. Wherever it makes sense, you simply add a call tothis.play()
aftersetMusic
and it’s effectively the same thing (albeit safer).In the case that someone clicks on a music entry in a playlist (whether the player is playing or stopped), the code already handles this by assuming the intention is to play the music (see this line).
That said, it would be useful to add an API for determining whether the APlayer instance was playing music or not. In that case, however, I’d suggest adding a member function like this:
That would be faster than checking the state of the DOM…
Recommend closing this issue as it was fixed by #174.