Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
Draw video on canvas ios can't autoplay

This code works fine on mac Add onload:main() to the body tag Can run automatically after startup In the ios browser can not run automatically, after setting a button, click to point to main(). Click to play normally under ios onload cannot be executed

Both mac and ios use safari browser. How to make ios autoplay normal


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 content

<body onload="main()">
 <center> 
 <canvas id="video"></canvas>
 </center>
 </body>
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

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

var VIDEO_CANVAS, interval;

document.addEventListener('DOMContentLoaded', e => {
  VIDEO_CANVAS = document.getElementById('video');
  const video = document.createElement('video');
  video.defaultPlaybackRate = .4;
  video.muted = true; // important for autoplay
  
  video.addEventListener('canplaythrough', e => {
    VIDEO_CANVAS.width = e.target.videoWidth * (200 / e.target.videoHeight);
    VIDEO_CANVAS.height = 200;
    
    e.target.play();
    interval = setInterval(function() {showVideoFrame(video);}, 30);
  });
  video.addEventListener('ended', e => {
    clearInterval(interval);
  });
  
  video.src = "https://mdn.github.io/learning-area/html/multimedia-and-embedding/video-and-audio-content/rabbit320.mp4";
});

function showVideoFrame(video) {
  const context = VIDEO_CANVAS.getContext("2d");
  context.drawImage(video, 0, 0, VIDEO_CANVAS.width, VIDEO_CANVAS.height);
}
<canvas id="video"></canvas>

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda