javascript - Change the embedded YouTube player which is affected by custom buttons -
in document, have 2 separate embedded youtube players. first has id player1
, second has id player2
.
<div id="1" class="track"> <iframe width="560" height="315" src="//www.youtube.com/embed/1g4isv_fylg?enablejsapi=1" frameborder="0" allowfullscreen id="player1"></iframe> </div> <div id="2" class="track"> <iframe width="560" height="315" src="//www.youtube.com/embed/fxsovfzyx28?enablejsapi=1" frameborder="0" allowfullscreen id="player2"></iframe> </div>
i have 3 buttons: "play", "pause" , "skip".
<div id="play-button">play</div> <div id="pause-button">pause</div> <div id="skip-button">skip</div>
i want skip
button change video affected play
, pause
buttons.
at moment have this:
js:
// inject youtube api script var tag = document.createelement('script'); tag.src = "//www.youtube.com/player_api"; var firstscripttag = document.getelementsbytagname('script')[0]; firstscripttag.parentnode.insertbefore(tag, firstscripttag); var player; var playernumber = 1; function onyoutubeplayerapiready() { // create global player specific iframe player = new yt.player('player' + playernumber, { events: { // call function when player ready use 'onready': onplayerready } }); } function onplayerready(event) { var playbutton = document.getelementbyid("play-button"); playbutton.addeventlistener("click", function() { player.playvideo(); }); var pausebutton = document.getelementbyid("pause-button"); pausebutton.addeventlistener("click", function() { player.pausevideo(); }); var skipbutton = document.getelementbyid("skip-button"); skipbutton.addeventlistener("click", function() { player.stopvideo(); playernumber = 2; }); }
my problem changing playernumber
within 'onready'
function not change player. need way of letting skip
button change player affected play
, pause
buttons.
there answer similar question here, question there refers 2 players, solution given there requires long declaration each player, player1 = new yt.player('player1'...
etc, , page have many players on eventually, prefer if change playernumber
variable. plan on giving further players ids player3
, player4
etc.
as beginner, apologise if have used terminology incorrectly, , hugely grateful may offered. thank you.
Comments
Post a Comment