2012-02-01 137 views
0

此腳本完美地工作,但它所做的是停止正在播放視頻的實例,如果新視頻啓動。當函數stopAllButMe();被調用時,我將如何重寫該函數以停止播放視頻的所有實例?jQuery停止視頻腳本

function stopAllButMe(playerState, player) { 
    if (playerState=='PLAYING') { 
     var myId = player.getId(); 
     projekktor('*').each(function() { 
      if (this.getId()!==myId) { 
       this.setStop(); 
      } 
     }); 
    } 
} 

projekktor('*').each(function() { 
    this.addListener('state', stopAllButMe); 
}); 

回答

0
function stopAllButMe(playerState) { 
    if (playerState=='PLAYING') { 
    projekktor('*').each(function() { 

     this.setStop(); 

    }); 
    } 
} 
projekktor('*').each(function() { 
    this.addListener('state', stopAllButMe); 
}); 

的 '如果' 語句檢查對輸入。

+0

這種作品...除了你甚至不能在視頻上點擊播放,因爲腳本自動立即停止它。 – Blainer 2012-02-01 22:25:14

+0

有沒有辦法讓這個腳本只在被調用時才運行? – Blainer 2012-02-01 22:28:54

0
function stopAllButMe (state, player) { 

    //iterate through each of the `<video>` elements 
    $.each($('video'), function() { 

     //check if the current iteration is the same element that got passed into this function 
     if (this.id != player.id) { 

      //stop any `<video>` element that isn't the one passed into this function 
      this.stop(); 
     } 
    }); 
} 

//find each `<video>` element and add an event handler for the `state` event 
$('video').bind('state', stopAllButMe); 

這假設每個<video>標籤都有自己的唯一ID。