2017-05-29 192 views
0

如何在視頻打開和準備狀態時暫停視頻?如何在視頻處於打開狀態和準備狀態時暫停視頻?

我的意思是說,如果我打開視頻並按下智能中心,並啓動其他應用程序視頻,以便在視頻處於打開或準備狀態時如何暫停視頻。

我正在使用此代碼進行多任務處理。

document.addEventListener('visibilitychange', function() { 
if(document.hidden){ 
     webapis.avplay.suspend(); //Mandatory. If you use avplay, you should call this method. 
} else { 
     webapis.avplay.restore(); 
}}); 

當視頻處於打開或準備狀態時,視頻將播放而不是掛起。

回答

0

首先檢查'visibilitychange'功能是否觸發您的計劃。

document.addEventListener('visibilitychange', function() { 
if(document.hidden){ 
    alert("To Suspend Video now"); 
} else { 
     alert("To Restore Video now"); 
}}); 

如果那好吧然後去文檔throught的暫停()的AVPlay API

文檔狀態約束:

被稱爲在這些國家 - 「暫停」, 「播放中」

使用getState()函數來確保狀態。

也提到了異常。捕獲拋出的異常以進行調試。

document.addEventListener('visibilitychange', function() { 

    console.log("Player state: " + webapis.avplay.getState()); 
    if(webapis.avplay.getState() === "PLAYING")||(webapis.avplay.getState() === "PAUSED"){ 

     if(document.hidden){ 
      try { 
       console.log("In Suspend block"); 
       webapis.avplay.suspend(); 
      } catch (e) { 
       console.error("Error in Suspend:"+e); 
      } 
     } 
     else { 
      try { 
       console.log("In Restore block"); 
       webapis.avplay.restore(); 
      } catch (e) { 
       console.error("Error in Restore:"+e); 
      } 

     } 
    } 
    else 
     console.log("Constraint not met"); 
});