禁用 JPlayer 的自动启动

huangapple go评论72阅读模式
英文:

Disable auto start in JPlayer

问题

I can translate the non-code part of your text:

"My theme is using JPlayer for a music player, but there is a problem that sometimes the music starts to play for no reason, and I didn't click on the play button. Sometimes I pause the player and refresh the page or move to another page, and the player starts from the beginning. How can I fix this and disable autoplay for my player?"

Please note that I've excluded the code section from the translation as you requested.

英文:

my theme is using Jplayer for a music player. but there is a problem that sometimes the music starts to play for no reason and I didn't click on the play button. sometimes I pause the player and refresh the page or move to another page, and the player starts from the beginning. How can I fix this and disable autoplay for my player?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

jQuery(document).ready(function(a) {
&quot;use strict&quot;;
function s(a) {
for (var s = a + &quot;=&quot;, t = document.cookie.split(&quot;;&quot;), e = 0; e &lt; t.length; e++) {
for (var i = t[e];
&quot; &quot; == i.charAt(0);) i = i.substring(1);
if (0 == i.indexOf(s)) return i.substring(s.length, i.length)
}
return &quot;&quot;
}
function t(a, s, t) {
var e = new Date;
e.setTime(e.getTime() + 24 * t * 60 * 60 * 1e3);
var i = &quot;expires=&quot; + e.toUTCString();
document.cookie = a + &quot;=&quot; + s + &quot;;&quot; + i + &quot;;path=/&quot;
}
jQuery(function(e) {
if (e(&quot;.audio-player&quot;).length) {
var i = &quot;&quot;,
r = new jPlayerPlaylist({
jPlayer: &quot;#jquery_jplayer_1&quot;,
cssSelectorAncestor: &quot;#jp_container_1&quot;
}, [], {
swfPath: &quot;js/plugins&quot;,
supplied: &quot;oga, mp3&quot;,
wmode: &quot;window&quot;,
useStateClassSkin: !0,
autoBlur: !1,
smoothPlayBar: !0,
keyEnabled: !0,
playlistOptions: {
autoPlay: false,
enableRemoveControls: !0
},
});
e(&quot;.play_hover&quot;).on(&quot;click&quot;, function() {
var a = e(this).closest(&quot;ul&quot;).children(&quot;li:eq(1)&quot;).find(&quot;a&quot;).attr(&quot;data-musicid&quot;),
s = e(this).closest(&quot;ul&quot;),
t = &quot;musicid=&quot; + a;
t += &quot;&amp;action=miraculous_play_single_music_action&quot;, e(&quot;.ms_ajax_loader&quot;).show(), e.ajax({
type: &quot;post&quot;,
url: frontadminajax.ajax_url,
data: t,
success: function(a) {
var t = JSON.parse(a);
&quot;false&quot; != t.status ? (r.add({
image: t.image,
title: t.song_name,
artist: t.artists,
mp3: t.mp3url,
id: t.mid,
share: t.share_uri,
option: i
}), e(&quot;.ms_ajax_loader&quot;).hide(), r.play(-1), e(&quot;.ms_list_songs&quot;).removeClass(&quot;play_active_song&quot;), e(&quot;.ms_player_wrapper&quot;).removeClass(&quot;close_player&quot;), e(s).addClass(&quot;play_active_song&quot;), localStorage.setItem(&quot;jp_playlist&quot;, JSON.stringify(r))) : (e(&quot;.ms_ajax_loader&quot;).hide(), toastr.error(t.msg))
}
})
}), e(&quot;.play_single_music&quot;).on(&quot;click&quot;, function() {
var a = e(this).closest(&quot;ul&quot;).children(&quot;li:eq(1)&quot;).find(&quot;a&quot;).attr(&quot;data-musicid&quot;),
s = e(this).closest(&quot;ul&quot;),
t = &quot;musicid=&quot; + a;
t += &quot;&amp;action=miraculous_play_single_music_action&quot;, e(&quot;.ms_ajax_loader&quot;).show(), e.ajax({
type: &quot;post&quot;,
url: frontadminajax.ajax_url,
data: t,
success: function(a) {
var t = JSON.parse(a);
&quot;false&quot; != t.status ? (r.add({
image: t.image,
title: t.song_name,
artist: t.artists,
mp3: t.mp3url,
id: t.mid,
share: t.share_uri,
option: i
}), e(&quot;.ms_ajax_loader&quot;).hide(), r.play(-1), e(&quot;.ms_list_songs&quot;).removeClass(&quot;play_active_song&quot;), e(&quot;.ms_player_wrapper&quot;).removeClass(&quot;close_player&quot;), e(s).addClass(&quot;play_active_song&quot;), localStorage.setItem(&quot;jp_playlist&quot;, JSON.stringify(r))) : (e(&quot;.ms_ajax_loader&quot;).hide(), toastr.error(t.msg))
}
})
}), e(&quot;.play_list_music&quot;).on(&quot;click&quot;, function() {
var a = &quot;playlist=&quot; + e(this).attr(&quot;data-list&quot;);
a += &quot;&amp;action=miraculous_play_user_playlist&quot;, e(&quot;.ms_ajax_loader&quot;).show(), e.ajax({
type: &quot;post&quot;,
url: frontadminajax.ajax_url,
data: a,
success: function(a) {
var s = JSON.parse(a);
&quot;false&quot; != s.status ? (e.each(s, function(a, s) {
r.add({
image: s.image,
title: s.song_name,
artist: s.artists,
mp3: s.mp3url,
id: s.mid,
share: s.share_uri,
option: i
})
}), e(&quot;.ms_ajax_loader&quot;).hide(), r.play(0), e(&quot;.ms_player_wrapper&quot;).removeClass(&quot;close_player&quot;), localStorage.setItem(&quot;jp_playlist&quot;, JSON.stringify(r))) : (e(&quot;.ms_ajax_loader&quot;).hide(), toastr.error(s.msg))
}
})
}), e(document).on(&quot;click&quot;, &quot;.play_music&quot;, function() {
e(this).removeClass(&quot;play_music&quot;),
e(this).addClass(&quot;pause_music btn_pause&quot;);
var a = e(this).attr(&quot;data-musicid&quot;),
s = e(this).attr(&quot;data-musictype&quot;),
t = &quot;musicid=&quot; + a + &quot;&amp;musictype=&quot; + s;
t += &quot;&amp;action=miraculous_play_all_music_action&quot;,
e(&quot;.ms_ajax_loader&quot;).show(),
r.remove(0),
e.ajax({
type: &quot;post&quot;,
url: frontadminajax.ajax_url,
data: t,
success: function(a) {
var t = JSON.parse(a);
&quot;false&quot; != t.status ? (e.each(t, function(a, s) {
r.add({
image: s.image,
title: s.song_name,
artist: s.artists,
mp3: s.mp3url,
id: s.mid,
share: s.share_uri,
option: i
})   
}), e(&quot;.ms_ajax_loader&quot;).hide(), &quot;radio&quot; == s &amp;&amp; e(&quot;.audio-player&quot;).addClass(&quot;ms_played_radio&quot;),
r.play(0),
e(&quot;.ms_list_songs&quot;).removeClass(&quot;play_active_song&quot;),
e(&quot;.ms_player_wrapper&quot;).removeClass(&quot;close_player&quot;),
localStorage.setItem(&quot;jp_playlist&quot;, JSON.stringify(r))) : toastr.error(t.msg)
}
}) 
}), e(document).on(&quot;click&quot;, &quot;.pause_music&quot;, function() {
e(this).removeClass(&quot;pause_music btn_pause&quot;), e(this).addClass(&quot;play_music&quot;), e(&quot;#jquery_jplayer_1&quot;).jPlayer(&quot;pause&quot;)
}),
e(&quot;.add_to_queue&quot;).on(&quot;click&quot;, function() {
var a = &quot;musicid=&quot; + e(this).attr(&quot;data-musicid&quot;) + &quot;&amp;musictype=&quot; + e(this).attr(&quot;data-musictype&quot;);
a += &quot;&amp;action=miraculous_add_to_queue_action&quot;, e(&quot;.ms_ajax_loader&quot;).show(), e.ajax({
type: &quot;post&quot;,
url: frontadminajax.ajax_url,
data: a,
success: function(a) {
var s = JSON.parse(a);
&quot;false&quot; != s.status ? (e.each(s, function(a, s) {
0 == a &amp;&amp; e(&quot;.jp-now-playing&quot;).html().length &lt;= 0 &amp;&amp; (e(&quot;.jp-now-playing&quot;).html(&quot;&lt;div class=&#39;jp-track-name&#39;&gt;&lt;span class=&#39;que_img&#39;&gt;&lt;img src=&#39;&quot; + s.image + &quot;&#39;&gt;&lt;/span&gt;&lt;div class=&#39;que_data&#39;&gt;&quot; + s.song_name + &quot; &lt;div class=&#39;jp-artist-name&#39;&gt;&quot; + s.artists + &quot;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&quot;), e(&quot;.jp_cur_download&quot;).attr(&quot;data-msmusic&quot;, s.mid), e(&quot;.jp_cur_favourite&quot;).attr(&quot;data-musicid&quot;, s.mid), e(&quot;.jp_cur_playlist&quot;).attr(&quot;data-msmusic&quot;, s.mid), e(&quot;.jp_cur_share&quot;).attr(&quot;data-shareuri&quot;, s.share_uri), e(&quot;.jp_cur_share&quot;).attr(&quot;data-sharename&quot;, s.song_name)), r.add({
image: s.image,
title: s.song_name,
artist: s.artists,
mp3: s.mp3url,
id: s.mid,
share: s.share_uri,
option: i
})
}), e(&quot;.ms_ajax_loader&quot;).hide(), e(&quot;.ms_player_wrapper&quot;).removeClass(&quot;close_player&quot;), toastr.success(&quot;Added.&quot;)) : toastr.error(s.msg)
}
})
}), e(&quot;.save_queue_list&quot;).on(&quot;click&quot;, function() {
var a = JSON.stringify(r),
s = &quot;musiclist=&quot; + a;
s += &quot;&amp;action=miraculous_user_queue_data_action&quot;, e(&quot;.ms_ajax_loader&quot;).show(), a ? e.ajax({
type: &quot;post&quot;,
url: frontadminajax.ajax_url,
data: s,
success: function(a) {
var s = JSON.parse(a);
&quot;success&quot; == s.status ? toastr.success(s.msg) : toastr.error(s.msg), e(&quot;.ms_ajax_loader&quot;).hide()
}
}) : toastr.error(&quot;Empty.&quot;)
}), e(&quot;.ms_remove_all&quot;).on(&quot;click&quot;, function() {
r.remove(), localStorage.removeItem(&quot;jp_playlist&quot;), e(&quot;#jquery_jplayer_1&quot;).jPlayer(&quot;clearMedia&quot;), toastr.success(&quot;Removed all from Queue&quot;), e(&quot;#clear_modal&quot;).modal(&quot;hide&quot;), e(&quot;.ms_list_songs&quot;).removeClass(&quot;play_active_song&quot;), e(&quot;.ms_empty_queue&quot;).html(&quot;Queue is empty&quot;), e(&quot;.jp-now-playing&quot;).html(&quot;&quot;), e(&quot;.ms_player_wrapper&quot;).addClass(&quot;close_player&quot;)
}), e(&quot;.ms_cancel&quot;).on(&quot;click&quot;, function() {
e(&quot;#clear_modal&quot;).modal(&quot;hide&quot;)
}), e(window).on(&quot;load&quot;, function() {
var a = JSON.parse(localStorage.getItem(&quot;jp_playlist&quot;));
localStorage.getItem(&quot;current_song&quot;, l);
if (a) {
var t = a.playlist,
l = a.current;
e.each(t, function(a, s) {
r.add({
image: s.image,
title: s.title,
artist: s.artist,
mp3: s.mp3,
id: s.id,
share: s.share,
option: i
}), a == l &amp;&amp; (e(&quot;.jp-now-playing&quot;).html(&quot;&lt;div class=&#39;jp-track-name&#39;&gt;&lt;span class=&#39;que_img&#39;&gt;&lt;img src=&#39;&quot; + s.image + &quot;&#39;&gt;&lt;/span&gt;&lt;div class=&#39;que_data&#39;&gt;&quot; + s.title + &quot; &lt;div class=&#39;jp-artist-name&#39;&gt;&quot; + s.artist + &quot;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&quot;), e(&quot;.jp_cur_download&quot;).attr(&quot;data-msmusic&quot;, s.id), e(&quot;.jp_cur_favourite&quot;).attr(&quot;data-musicid&quot;, s.id), e(&quot;.jp_cur_playlist&quot;).attr(&quot;data-msmusic&quot;, s.id), e(&quot;.jp_cur_share&quot;).attr(&quot;data-shareuri&quot;, s.share), e(&quot;.jp_cur_share&quot;).attr(&quot;data-sharename&quot;, s.title)), localStorage.setItem(&quot;current_song&quot;, l)
});
var o = s(&quot;MJTCURRENTTIME&quot;),
n = s(&quot;MJTCURRENTSONG&quot;);
&quot;&quot; != o &amp;&amp; &quot;&quot; != n &amp;&amp; r.play(n, parseInt(o))
} else {
var u = &quot;musiclist=queue&quot;;
u += &quot;&amp;action=miraculous_user_load_queue_data_action&quot;, e.ajax({
type: &quot;post&quot;,
url: frontadminajax.ajax_url,
data: u,
success: function(a) {
var s = JSON.parse(a);
if (&quot;success&quot; == s.status) {
var t = JSON.parse(s.queue_data);
if (t) {
var l = t.playlist,
o = t.current;
e.each(l, function(a, s) {
r.add({
image: s.image,
title: s.title,
artist: s.artist,
mp3: s.mp3,
id: s.id,
share: s.share,
option: i
}), a == o &amp;&amp; (e(&quot;.jp-now-playing&quot;).html(&quot;&lt;div class=&#39;jp-track-name&#39;&gt;&lt;span class=&#39;que_img&#39;&gt;&lt;img src=&#39;&quot; + s.image + &quot;&#39;&gt;&lt;/span&gt;&lt;div class=&#39;que_data&#39;&gt;&quot; + s.title + &quot; &lt;div class=&#39;jp-artist-name&#39;&gt;&quot; + s.artist + &quot;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&quot;), e(&quot;.jp_cur_download&quot;).attr(&quot;data-msmusic&quot;, s.id), e(&quot;.jp_cur_favourite&quot;).attr(&quot;data-musicid&quot;, s.id), e(&quot;.jp_cur_playlist&quot;).attr(&quot;data-msmusic&quot;, s.id), e(&quot;.jp_cur_share&quot;).attr(&quot;data-shareuri&quot;, s.share), e(&quot;.jp_cur_share&quot;).attr(&quot;data-sharename&quot;, s.title)), localStorage.setItem(&quot;current_song&quot;, o)
})
}
}
&quot;default&quot; == s.status &amp;&amp; (r.add({
image: s.image,
title: s.song_name,
artist: s.artists,
mp3: s.mp3url,
id: s.mid,
share: s.share_uri,
option: i
}), e(&quot;.jp-now-playing&quot;).html(&quot;&lt;div class=&#39;jp-track-name&#39;&gt;&lt;span class=&#39;que_img&#39;&gt;&lt;img src=&#39;&quot; + s.image + &quot;&#39;&gt;&lt;/span&gt;&lt;div class=&#39;que_data&#39;&gt;&quot; + s.song_name + &quot; &lt;div class=&#39;jp-artist-name&#39;&gt;&quot; + s.artists + &quot;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&quot;), e(&quot;.jp_cur_download&quot;).attr(&quot;data-msmusic&quot;, s.mid), e(&quot;.jp_cur_favourite&quot;).attr(&quot;data-musicid&quot;, s.mid), e(&quot;.jp_cur_playlist&quot;).attr(&quot;data-msmusic&quot;, s.mid), e(&quot;.jp_cur_share&quot;).attr(&quot;data-shareuri&quot;, s.share_uri), e(&quot;.jp_cur_share&quot;).attr(&quot;data-sharename&quot;, s.song_name))
}
})
}
}), e(&quot;#jquery_jplayer_1&quot;).on(e.jPlayer.event.ready + &quot; &quot; + e.jPlayer.event.play, function(a) {
var s = r.current,
t = r.playlist;
e.each(t, function(a, t) {
a == s &amp;&amp; (e(&quot;.jp-now-playing&quot;).html(&quot;&lt;div class=&#39;jp-track-name&#39;&gt;&lt;span class=&#39;que_img&#39;&gt;&lt;img src=&#39;&quot; + t.image + &quot;&#39;&gt;&lt;/span&gt;&lt;div class=&#39;que_data&#39;&gt;&quot; + t.title + &quot; &lt;div class=&#39;jp-artist-name&#39;&gt;&quot; + t.artist + &quot;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&quot;), e(&quot;.jp_cur_download&quot;).attr(&quot;data-msmusic&quot;, t.id), e(&quot;.jp_cur_favourite&quot;).attr(&quot;data-musicid&quot;, t.id), e(&quot;.jp_cur_playlist&quot;).attr(&quot;data-msmusic&quot;, t.id), e(&quot;.jp_cur_share&quot;).attr(&quot;data-shareuri&quot;, t.share), e(&quot;.jp_cur_share&quot;).attr(&quot;data-sharename&quot;, t.title)), localStorage.setItem(&quot;current_song&quot;, s), i = &quot;musicid=&quot; + t.id, i += &quot;&amp;action=miraculous_play_single_music_action&quot;, e.ajax({
type: &quot;post&quot;,
url: frontadminajax.ajax_url,
data: i,
success: function(a) {}
})
}), e(&quot;.knob-wrapper&quot;).mousedown(function() {
return e(window).mousemove(function(a) {
var s = function(a) {
var s = a.css(&quot;-webkit-transform&quot;) || a.css(&quot;-moz-transform&quot;) || a.css(&quot;-ms-transform&quot;) || a.css(&quot;-o-transform&quot;) || a.css(&quot;transform&quot;);
if (&quot;none&quot; !== s) var t = s.split(&quot;(&quot;)[1].split(&quot;)&quot;)[0].split(&quot;,&quot;),
e = t[0],
i = t[1],
r = Math.round(Math.atan2(i, e) * (180 / Math.PI));
else r = 0;
return r &lt; 0 ? r + 360 : r
}(e(&quot;.knob&quot;)) / 270;
s &gt; 1 ? e(&quot;#jquery_jplayer_1&quot;).jPlayer(&quot;volume&quot;, 1) : s &lt;= 0 ? e(&quot;#jquery_jplayer_1&quot;).jPlayer(&quot;mute&quot;) : (e(&quot;#jquery_jplayer_1&quot;).jPlayer(&quot;volume&quot;, s), e(&quot;#jquery_jplayer_1&quot;).jPlayer(&quot;unmute&quot;))
}), !1
}).mouseup(function() {
e(window).unbind(&quot;mousemove&quot;)
});
var i = !1;
e(&quot;.jp-play-bar&quot;).mousedown(function(a) {
i = !0, l(a.pageX)
}), e(document).mouseup(function(a) {
i &amp;&amp; (i = !1, l(a.pageX))
}), e(document).mousemove(function(a) {
i &amp;&amp; l(a.pageX)
});
var l = function(a) {
var s = e(&quot;.jp-progress&quot;),
t = 100 * (a - s.offset().left) / s.width();
t &gt; 100 &amp;&amp; (t = 100), t &lt; 0 &amp;&amp; (t = 0), e(&quot;#jquery_jplayer_1&quot;).jPlayer(&quot;playHead&quot;, t), e(&quot;.jp-play-bar&quot;).css(&quot;width&quot;, t + &quot;%&quot;)
};
e(&quot;#playlist-toggle, #playlist-text, #playlist-wrap li a&quot;).unbind().on(&quot;click&quot;, function() {
e(&quot;#playlist-wrap&quot;).fadeToggle(), e(&quot;#playlist-toggle, #playlist-text&quot;).toggleClass(&quot;playlist-is-visible&quot;)
}), e(&quot;.hide_player&quot;).unbind().on(&quot;click&quot;, function() {
e(&quot;.audio-player&quot;).toggleClass(&quot;is_hidden&quot;), e(this).html(&#39;&lt;i class=&quot;fa fa-angle-down&quot;&gt;&lt;/i&gt; HIDE&#39; == e(this).html() ? &#39;&lt;i class=&quot;fa fa-angle-up&quot;&gt;&lt;/i&gt; SHOW PLAYER&#39; : &#39;&lt;i class=&quot;fa fa-angle-down&quot;&gt;&lt;/i&gt; HIDE&#39;)
}), e(&quot;body&quot;).unbind().on(&quot;click&quot;, &quot;.audio-play-btn&quot;, function() {
e(&quot;.audio-play-btn&quot;).removeClass(&quot;is_playing&quot;), e(this).addClass(&quot;is_playing&quot;);
var a = e(this).data(&quot;playlist-id&quot;);
r.play(a)
})
}), a(&quot;#jquery_jplayer_1&quot;).bind(a.jPlayer.event.play, function(s) {
t(&quot;MJTCURRENTSONG&quot;, r.current, 1), a(&quot;#jquery_jplayer_1&quot;).bind(a.jPlayer.event.timeupdate, function(a) {
t(&quot;MJTCURRENTTIME&quot;, Math.trunc(a.jPlayer.status.currentTime), 1)
})
})
}
})
});

<!-- end snippet -->

答案1

得分: 1

JPlayer演示页面的“控制标志描述”部分

autoPlay:布尔值:在页面上实例化时,将自动播放,并在使用新播放列表时

在您的代码中,它的值为true。可能这是一个问题。

更新:

我还看到play()操作在window的**“load”**事件处理程序中。如果LocalStorage中有有关播放列表的信息,它将被调用。因此,看起来像是您的情景。

英文:

On JPlayer demo page in "control flag description" section

> autoPlay : Boolean : Will auto-play when instanced on the page, and when a new playlist is given using

It has true value in your code. Probably that is a problem.

UPD:

Also i see play() action in window "load" event handler. It is called if LocalStorage has information about playlist. So it looks like your scenario

答案2

得分: 0

将变量设置为""!0""将其设置为""true""。这就是为什么您的脚本会自动播放音乐。在jPlayer文档中,自动播放选项写成如下形式:

playlistOptions: {
  autoPlay: false,
  enableRemoveControls: false
}
英文:

Setting a variable to "!0" will set it to "true". This is why your script autoplay the music. In the jPlayer documentation, autoplay option is written like this:

playlistOptions: {
autoPlay: false,
enableRemoveControls: false
}

huangapple
  • 本文由 发表于 2020年1月3日 23:48:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定