Issues Playing and pausing tracks from a playlist (Soundcloud JavaScript API)

牧云@^-^@ 提交于 2019-12-11 07:29:22

问题


I'm creating an app using Soundcloud's JavaScript API that takes a URL to a Soundcloud playlist and displays the track list next to an audio player. I'm able to fetch everything using the API and play individual tracks using SC.stream(), but I'm running into a strange issue:

The first time you play a track, the Play and Pause buttons work as expected. If you select a new track, then go back and select the first track again, I would expect it to start over from the beginning. Instead, it remembers where it left off and the pause button doesn't work. If you hit the Play button while the audio is playing, the pause button works again.

How do I get SC.stream() to make a fresh request (start from the beginning of the track and reset the player object) when selecting the same track twice?

Below is a stripped down version of this working example on JSFiddle.

playTrack = function(id) {
  SC.stream("/tracks/" + id).then(function(player) {
    player.play();

    $(".player__play").click(function(e) {
      e.preventDefault();
      player.play();
    });

    $(".player__pause").click(function(e) {
      e.preventDefault();
      player.pause();
    });
  });
};

// Play a track from the playlist
$(document).on("click", ".playlist a", function(e) {
  e.preventDefault();
  var trackId = $(e.currentTarget).attr("data-track-id");
  playTrack(trackId);
});

来源:https://stackoverflow.com/questions/40074942/issues-playing-and-pausing-tracks-from-a-playlist-soundcloud-javascript-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!