當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


p5.js MediaElement time()用法及代碼示例

p5.j​​s中p5.MediaElement的time()方法用於設置或返回媒體元素的當前時間。它接受一個參數,該參數表示媒體應跳至或跳至的時間(以秒為單位)。可以通過不向函數傳遞任何參數來返回媒體的當前時間。

用法:

time( time )

參數:此方法接受如上所述和以下描述的單個參數:

  • time:它是一個數字,指定媒體應跳至的時間(以秒為單位)。它是一個可選參數。

返回值:此方法返回一個Number,表示媒體元素的當前時間。

以下示例說明了p5.js中的time()方法:



例:

Javascript

function setup() { 
    createCanvas(500, 400); 
    textSize(18); 
  
    text("Click on the buttons to skip " + 
         "the video forward or backward", 
         20, 20); 
  
    example_media = 
      createVideo("sample-video.mp4"); 
    example_media.size(500, 300); 
    example_media.position(20, 100); 
    example_media.showControls(); 
  
    skipbwdBtn = 
      createButton("Skip Back 2 Seconds"); 
    skipbwdBtn.position(30, 40); 
    skipbwdBtn.mousePressed(skipBackward); 
  
    skipfwdBtn = 
      createButton("Skip Forward 2 Seconds"); 
    skipfwdBtn.position(200, 40); 
    skipfwdBtn.mousePressed(skipForward); 
} 
  
function skipForward() { 
  clear(); 
  
  // Get the current time 
  let currTime = example_media.time(); 
  
  // Add 2 seconds to skip forward 
  let fwdTime = currTime + 2; 
  
  // Set the new time that is two seconds 
  // after the current time 
  example_media.time(fwdTime); 
  
  text("The video has been skipped forward", 
       20, 80); 
  
  text("Click on the buttons to skip " +  
       "the video forward or backward", 
       20, 20); 
} 
  
function skipBackward() { 
  clear(); 
  
  // Get the current time 
  let currTime = example_media.time(); 
  
  // Subtract 2 seconds to skip back 
  let fwdTime = currTime - 2; 
  
  // Set the new time that is two seconds 
  // before the current time 
  example_media.time(fwdTime); 
  
  text("The video has been skipped back", 
       20, 80); 
  
  text("Click on the buttons to skip " + 
       "the video forward or backward", 
       20, 20); 
}

輸出:

在線編輯: https://editor.p5js.org/
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考:https://p5js.org/reference/#/p5.MediaElement/time

相關用法


注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.MediaElement time() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。