在Canvas Ios上绘制视频可以播放自动播放
此代码在Mac上正常工作 将Onload:Main()添加到身体标签 可以在启动后自动运行 在iOS浏览器中,设置按钮后无法自动运行,单击以指向main()。点击正常玩 在iOS下 无法执行
Mac和iOS使用Safari浏览器。如何使iOS自动播放正常
function main() {
const video1= initializeVideo("mv/m4.mp4");
VIDEO_CANVAS = document.getElementById('video');
video1.addEventListener('loadeddata', (event) => {
const context = VIDEO_CANVAS.getContext("2d");
context.canvas.width = video1.videoWidth * (740 / video1.videoHeight);
context.canvas.height = 740;
processVideos(video1,0);
});
}
function processVideos(video1,frame1){
if(frame1 <=200 )
{ showVideoFrame(video1,frame1); frame1++; }
if(frame1>=200) {
location.reload();
}
setTimeout(function(){
processVideos(video1,frame1);
},30);
}
function showVideoFrame(video,frame){
var time = frame / 30 ;
video.currentTime=time;
const context=VIDEO_CANVAS.getContext("2d");
context.drawImage(video, 0, 0, VIDEO_CANVAS.width,VIDEO_CANVAS.height);
}
function initializeVideo(filename) {
var video = document.createElement('video');
video.src=filename;
video.load();
return video;
}
身体内容
<body onload="main()">
<center>
<canvas id="video"></canvas>
</center>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
视频的想法是慢动作吗?跳到框架并不容易,因为您不知道已加载了多少视频。在此示例中,我在视频上使用属性
defaultplaybackrate
来控制速度。然后只需播放视频,然后每30毫秒绘制当前的框架。视频结束后清除间隔。更新
如果要进行自动播放,则可以在播放视频之前尝试静音。另一种选择是让用户首先与页面进行交互,要么单击某些内容(单击事件)或滚动(滚动事件)。您可以在此处阅读更多有关它的信息:
Is the idea to slow motion the video? It is not easy to jump from frame to frame, because you don't know how much of the video has been loaded. In this example I use the property
defaultPlaybackRate
on the video to control the speed. And then simply play the video and draw the current frame to the canvas every 30 ms. The interval is cleared when the video has ended.Update
If this is going to autoplay you can try to mute the video before playing it. An alternative could be to let the user interact with the page first, either clicking something (click event) or scrolling (scroll event) a bit. You can read more about it here: Autoplay guide for media and Web Audio APIs - Web media technologies | MDN