p5.MediaElement time() 方法
p5.js 中 p5.MediaElement 的time() 方法用于设置或返回媒体元素的当前时间。它接受一个参数,该参数表示媒体应该跳转或跳过的时间(以秒为单位)。不向函数传递任何参数即可返回媒体的当前时间。
句法:
time( time )
参数:此方法接受如上所述和如下所述的单个参数:
- time:这是一个数字,指定媒体应该跳到的时间(以秒为单位)。它是一个可选参数。
返回值:此方法返回一个数字,表示媒体元素的当前时间。
下面的例子说明了 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