Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

javascript - How to save current playing song in cookie?

I am using jPlayer plugin. Here is an example link [ jsfiddle ].

I save the current playing track number in a cookie. After a browser reload it starts playing from the track number loaded from the cookie. This works but the problem is, after the current track ends, it starts to play from the first track of the playlist. I want it to continue to the next track.

Example of current situation:

  • playlist have 10 songs
  • clicked on number 3 song
  • cookie saved number 3 song
  • browser reload
  • start playing number 3 song from playlist
  • number 3 song ends
  • start playing from number 1 song

Desired situation:

  • playlist have 10 songs
  • clicked on number 3 song
  • cookie saved number 3 song
  • browser reload
  • start playing number 3 song from playlist
  • number 3 song ends
  • start playing number 4 song
  • number 4 song ends
  • start playing number 5 song
  • ...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

myPlayer.play() expects an integer value, but when you read from the cookie you get a string. So to fix your problem, just pass the value through parseInt(), like this:

myPlayer.play(parseInt(playnow));

You will notice that on page reload, the proper track will be selected in the playlist as it starts playing, and it will continue to the next track correctly.

Demo: http://jsfiddle.net/alan0xd7/6kx616hr/1/

By the way, I think setInterval with 1 (1 millisecond) is too much, maybe 1000 (1 second) is enough.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...