dongshubang7816 2018-06-28 11:14
浏览 146
已采纳

使用Amplitude.js继续下一页的歌曲

I am using Amplitude.js to play songs which are dynamic. Server side script is php. My goal is to check if any song is playing, if yes than before that page is navigated away then save that song's index and current position (in percentage). Then on the next page I have to continue that song from the same position. So far I am successful in playing the same song on the next page, but it is playing from the start.

Here $feat is an array created like :

$featured = array();
        foreach ($all as $key => $value) {
           array_push($featured, array(
                "id" => $value['audio_id'],
                "name" => $value['audio_name'],
                "artist" => $value['audio_artist'],
                "album" => $value['product_name'],
                "url" => l($value['audio_file_path'].$value['audio_file']),
                "cover_art_url" =($value['product_image_path'].$value['product_image']),
           ));
        }

My javascript code to intitialize Amplitude js:

<script>
Amplitude.setDebug( true );
let _songs = JSON.parse('<?=$feat?>');
Amplitude.init({
  "songs":_songs,
});
</script>

On the change of every page, I am saving the song current index and current percentage of the song played. Its working fine. Home/song_stats will save the song info in session

<script>
    $(window).on("beforeunload", function() {
    song_percent =  Amplitude.getSongPlayedPercentage();
    song_index = Amplitude.getActiveIndex();

    $.ajax({
      type: "POST",
      url: "<?=g('base_url')?>Home/song_stats", 
      data:  "song_percent="+song_percent+"&song_index="+song_index,
      dataType: "json"
    });
    return false;    
    });

</script>

This javascript code is playing the song on click and if any session data is available to continue the song, it will continue that song.

<script>
$(window).load(function(){

  function playmysongs(_index){
  $('div[amplitude-song-index="'+_index+'"]').click();
}


  var appender = '';
  for(var i =0;i<_songs.length;i++){
    appender+='<div class="rixinside-list"><a onclick="playmysongs('+i+')"><span class="album_img"><img src="'+_songs[i].cover_art_url+'" class="img-responsive"/></span><span class="album_name">'+_songs[i].album+'<span class="album_artist">'+_songs[i].artist+'</span></span></a></div>';
  }
$('#playlist-container .rixplaylist').append(appender);

  //cont song
     $.ajax({
      type: "POST",
      url: "<?=g('base_url')?>Home/continue_song",
      data:  "",
      dataType: "json",
      success: function(response){
        playmysongs(response.song_index);
        Amplitude.setSongPlayedPercentage(response.song_percent);
      }
    });
    return false;  
    //cont song end
});


$('#playlist-container').click(function(){
  $('.rixplaylist').toggle();
});
$('#wishlist-container i').click(function(){
  $(this).toggleClass('like');
});

The line below doesn't work, although "response.song_percent" is showing correct value. Any help will be appreciated. Amplitude.setSongPlayedPercentage(response.song_percent);

  • 写回答

2条回答 默认 最新

  • doushi5913 2018-06-29 19:54
    关注

    There are a few options using AmplitudeJS 3.3 or above.

    First, before navigating, I'd save the current position of the song being played to local storage or to a cookie. If you want to save the location server-side, you could do that as well. The local storage might just be easier if you are doing everything in Javascript.

    When loading the page, after you get your song played percentage (or current time) you have one of two options.

    1. Use Amplitude.setSongPlayedPercentage( percentage ).

    What this does is set the current song being played to the percentage defined. The key being current. Now if the user skips to song 3, goes to the next page, sets the song played percentage of 35, it's going to do the first song in the array which will be the wrong song. Before you do Amplitude.setSongPlayedPercentage, you will have to set the start_song key in the Amplitude.init() method to be the index of the song played by the user. Then you can run the Amplitude.setSongPlayedPercentage( percentage ) and it will work appropriately.

    2. Use Amplitude.skipTo( seconds, index );

    This method is a little newer, but a little less complex. What you will need to do is save the seconds into the song to either local storage, cookie, or however you are doing it along with the index of the song. When you load the page, after you've initialized AmplitudeJS, you can run, Amplitude.skipTo( seconds, index ). The seconds being how far into the song the user was and the index being the index of the song in the songs array. It will resume from there.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘