0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

HTML5動画:読込完了したら再生したい

Posted at

問題

HTML動画(短尺)

  • 途中から再生
  • スムーズに再生、シーク

のニーズがあるたくて、読込完了してから再生したくで、その調べた方法を簡単にまとめます。

方法

いろんな方法がありますが、ObjectURLが一番良さそうと思っています:

① ajaxで動画のBlobをダウンロード
② BlobからObjectURLを作成
③ 作成したObjectURLをに渡す

サンプルコード

AxiosとVueを使っていますが、ロジックが通用できます。

<template>
   <div v-if="blobUrl">
    <video muted autoplay controls >
      <source  v-bind:src="blobUrl" type="video/mp4" />
    </video>
  </div>
  <div v-else>
    loading...
  </div>
</template>

<script>

...
data: function () {
  return {
    blobUrl: null,
  }
},
created() {
  axios.get(this.videoUrl), {
    responseType: 'blob',
    timeout: 100000,
  }).then(response => {
    const blob = response.data;
    this.blobUrl = URL.createObjectURL(blob);
  });
}

...
</script>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?