Javascript 使用Amplitude.js在下一页继续播放歌曲
我正在使用Amplication.js播放动态歌曲。服务器端脚本是php。 我的目标是检查是否有任何歌曲正在播放,如果是,则将该页面导航到别处,然后保存该歌曲的索引和当前位置(百分比)。 然后在下一页,我必须从相同的位置继续这首歌。到目前为止,我成功地在下一页播放同一首歌曲,但它是从一开始播放 此处$feat是一个创建如下的数组:Javascript 使用Amplitude.js在下一页继续播放歌曲,javascript,php,ajax,Javascript,Php,Ajax,我正在使用Amplication.js播放动态歌曲。服务器端脚本是php。 我的目标是检查是否有任何歌曲正在播放,如果是,则将该页面导航到别处,然后保存该歌曲的索引和当前位置(百分比)。 然后在下一页,我必须从相同的位置继续这首歌。到目前为止,我成功地在下一页播放同一首歌曲,但它是从一开始播放 此处$feat是一个创建如下的数组: $featured = array(); foreach ($all as $key => $value) { array
$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']),
));
}
初始化振幅js的我的javascript代码:
<script>
Amplitude.setDebug( true );
let _songs = JSON.parse('<?=$feat?>');
Amplitude.init({
"songs":_songs,
});
</script>
振幅设置调试(真);
让_songs=JSON.parse(“”);
振幅.init({
“歌曲”:,
});
在每页的更改中,我都会保存歌曲当前索引和播放歌曲的当前百分比。工作正常。
Home/song_统计将在会话中保存歌曲信息
<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>
$(window).on(“beforeunload”,function()){
song_percent=振幅。getSongPlayedPercentage();
song_index=振幅。getActiveIndex();
$.ajax({
类型:“POST”,
url:“主页/歌曲统计”,
数据:“song_percent=“+song_percent+”&song_index=“+song_index,
数据类型:“json”
});
返回false;
});
此javascript代码在单击时播放歌曲,如果有任何会话数据可用于继续播放歌曲,它将继续播放该歌曲
<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');
});
$(窗口)。加载(函数(){
函数播放歌曲(_索引){
$('div[amplitude song index=“”+_index+“]”)。单击();
}
var appender='';
对于(var i=0;i可能存在异步问题:在设置百分比之前,尝试用代码行本身替换对“playmysongs”函数的调用,或者在设置百分比值之前添加200超时。使用AmplicateJS 3.3或更高版本有一些选项
首先,在导航之前,我会将正在播放的歌曲的当前位置保存到本地存储或cookie中。如果您想将位置保存到服务器端,也可以这样做。如果您使用Javascript执行所有操作,本地存储可能会更容易
加载页面时,在您获得歌曲播放百分比(或当前时间)后,您有两个选项之一
1.使用振幅。设置播放百分比(百分比)。
这样做的目的是将当前播放的歌曲设置为定义的百分比。键为当前。现在,如果用户跳到歌曲3,转到下一页,将歌曲播放百分比设置为35,则将执行数组中的第一首歌曲,这将是错误的歌曲。在执行Amplication.setSongPlayedPercentage之前,必须设置start_song
输入振幅.init()方法作为用户播放歌曲的索引。然后可以运行振幅.setSongPlayedPercentage(percentage),它将正常工作
2.使用振幅.跳跃(秒,索引);
此方法稍新,但复杂度稍低。您需要做的是将秒数保存到歌曲中的本地存储、cookie或与歌曲索引一起执行的方式。加载页面时,初始化AmplicateJS后,可以运行Amplication.skipTo(秒数,索引)。秒是指用户进入歌曲的距离,索引是歌曲数组中歌曲的索引。它将从此处恢复
希望这有帮助!另一种选择是在播放后使用回调:开始播放歌曲并if(response.song\u index>0)振幅。setSongPlayedPercentage(response.song\u index)
感谢您的帮助。不过,在新页面上播放最后一首播放的歌曲不是问题,我能够完成这项工作,但没有设置百分比。感谢您推荐本地存储,我使用了您的第二个选项Amplication.skipTo(秒,索引);它成功了!