SoundCloud SDK播放功能

为什么trackId在第一次播放时未定义?

跳过曲目后,播放功能按预期工作。

我使用SoundCloud Javascript SDK构建了一个播放器,并且遇到了一些问题。 当按下play时,变量trackId是未定义的。 但是,我可以跳过曲目以获得示例帐户中每首歌曲的成功播放。

*注意:我曾与HTML5小部件和api一起工作,但包含一个iframe,它不适用于iPhone音频播放,所以我需要使用JavaScript SDK解决这个问题。

提前致谢!

开发区域:http://dev-mountvalor.legendarylion.com/

文档:https://developers.soundcloud.com/docs/api/sdks#stream

  var songs=[{}];

   // function getPlaylist(){

   //  // currentTrack = songs[1].soundcloud_id;
   //  //  console.log(songs[1].soundcloud_id);
   //  //  console.log(currentTrack);
   // }

 var setList = 'testing-mountain/tracks';


SC.initialize({
            client_id: "ae0b7cf303a1217a43a57c3658e673ff"
        });

         SC.get("/resolve/?url=https://soundcloud.com/" + setList, {limit: 52}, function(userReturn){
            // console.log(userReturn);

            // console.log(userReturn.tracks[1].title);

            // console.log(userReturn.length);

              // iterate through the json and snag the stuff we need, create an associative array out of it
              for (var i = 0; i < userReturn.length; i++) {

                  songs.push(
                    {"title" : userReturn[i].title,
                    "song_url" : "https://soundcloud.com/",
                    "soundcloud_id" : userReturn[i].id, 
                    "artwork" : userReturn[i].artwork_url}
                    );
              }

              // Remove the proto addition to the object
              songs.shift();

              console.log(songs);
              // currentTrack=songs[1].soundcloud_id;
              // console.log(currentTrack);

                 // console.log(songs);
                 // console.log(songs[1].soundcloud_id);

                 // trackID = (songs[1].soundcloud_id);
                 // console.log('trackID is equal to '+ trackID);



              // $( "#track-title" ).empty();
              // $( "#track-title" ).append( "<p> Now Playing: " + userReturn[0].title + "</p><img class='player-thumb' src='" + userReturn[0].artwork_url + "' alt=''>" );

              // getPlaylist();

           });

    Track = function (trackId){
    //Testing mountain sample track
    // var trackId = '240347155';
    //Mount Valor sample track
    // var trackId = '241728584';

    // var setList = 'mountvalor/tracks';



        SC.stream("/tracks/" + trackId, function(sound){
            player = sound;
        });


        this.play = function() {
            player.play();            
        };

        this.pause = function() {
            player.pause();
        };

        this.stop = function() {
            player.stop();
        };
        // console.log(trackId);
    };


    Rotation = function(tracks) {
        var currentTrack = tracks[0];


        this.currentTrack = function () {
            return currentTrack;
        };

        this.nextTrack = function () {
            var currentIndex = tracks.indexOf(currentTrack);
            var nextTrackIndex = currentIndex + 1;
            var nextTrackId = tracks[nextTrackIndex];
            currentTrack = nextTrackId;
            console.log(currentTrack);
            console.log(currentIndex);
            $( "#track-title" ).html( "<p> Now Playing: " + currentTrack.title + "</p><img class='player-thumb' src='" + currentTrack.artwork + "' alt=''>" );
            return currentTrack;
        };

        this.backTrack = function () {
            var currentIndex = tracks.indexOf(currentTrack);
            var nextTrackIndex = currentIndex - 1;
            var nextTrackId = tracks[nextTrackIndex];
            currentTrack = nextTrackId;
            console.log(currentTrack);
            console.log(currentIndex);
            $( "#track-title" ).html( "<p> Now Playing: " + currentTrack.title + "</p><img class='player-thumb' src='" + currentTrack.artwork + "' alt=''>" );
            return currentTrack;
        };

    };

    $(document).ready (function(){

        $('#volume').on('change', function(event){
            player.setVolume($(this).val());
            // console.log($(this).val());
        });
        // console.log(songs);

        var rotation = new Rotation(songs);
        var currentTrack = rotation.currentTrack();
        var currentPlayingTrack = new Track(currentTrack.soundcloud_id);

        $('#play').on('click', function(event){
            currentTrack = rotation.currentTrack();
            currentPlayingTrack.play();
            $('#pause').show();
            $('#play').hide();
        });

      $('.hero-play-button').on('click', function(event){
          // console.log($(this).attr('id'));
          // grab the id of the current element clicked
          var circlePlayButtonId = ($(this).attr('id'));
          //replace the string in that element of the id to get the index counter from the id
          circlePlayButtonId = circlePlayButtonId.replace('circle-play-', '');
          // console.log('The replaced index variable is ' + circlePlayButtonId);
          //play the song based on the index pulled from the id when clicked
          // player.skip(circlePlayButtonId);
          $('#pause').show();
          $('#play').hide();
      });

        $('#pause').on('click', function(event){
            currentPlayingTrack.pause();
            $('#pause').hide();
            $('#play').show();
        });


        $('#next').on('click', function(event){
            currentPlayingTrack.stop();
            currentTrack = rotation.nextTrack();
            currentPlayingTrack = new Track(currentTrack.soundcloud_id);
            currentPlayingTrack.play();
            $('#play').hide();
            $('#pause').show();
        });
         $('#prev').on('click', function(event){
            currentPlayingTrack.stop();
            currentTrack = rotation.backTrack();
            currentPlayingTrack = new Track(currentTrack.soundcloud_id);
            currentPlayingTrack.play();
            $('#play').hide();
            $('#pause').show();
            console.log('backtrack!');
        });


    }); // End Document Ready

在歌曲已经加载时,您应该及时移动轨道和旋转对象的创建:

SC.get("/resolve/?url=https://soundcloud.com/" + setList, {limit: 52})
  .then(function(userReturn){

检查plunk

链接地址: http://www.djcxy.com/p/65305.html

上一篇: SoundCloud SDK Play Function

下一篇: SoundCloud widget volume gets reset to 100 when next song is played