Javascript stop YouTube video

SoundCloud players have a default functionality where when a user starts a player, it automatically pauses all others that are currently playing. [example: //jsfiddle.net/Vx6hM/].

I'm trying to recreate this with embedded youtube videos that are added dynamically via the normal embed code.

Using this: //stackoverflow.com/a/7513356/938089 and this: //stackoverflow.com/a/7988536/1470827

I'm able to get this far:

function chain[]{ $['.post'].each[function[]{ var player_id = $[this].children['iframe'].attr["id"]; var other_player_id = $[this].siblings[].children['iframe'].attr["id"]; player = new YT.Player[ player_id, { events: { 'onStateChange': function [event] { if [event.data == YT.PlayerState.PLAYING] { callPlayer[ other_player_id , 'pauseVideo' ]; alert['Trying to pause: ' + other_player_id]; } } } }]; }]; }

Here's a JS Bin with it half working: //jsbin.com/oxoyes/2/edit

Currently, its only calling one of the players. I need it to pause ALL of the other players except the playing one.

Also, any tips on how to clean this up and/or do it differently/better are welcome. I'm still very new to javascript so I'm not even sure I'm going about it the right way.

Thanks!

Video liên quan

Chủ Đề