javascript - vue.js 缓存视频并播放

标签 javascript node.js video vue.js

我对 vue.js 世界还很陌生,我对某个案例感到迷失。

我使用资源加载器做了一个小型缓存系统,它预加载我的图像和视频并将数据放入数组中。一切正常,但现在,我不知道如何创建我的视频元素并播放它。

我无法执行 v-for,因为有时它是图像,有时它是视频......

我不知道该怎么办!

这是我的 App.vue

<template>
  <div>

    <div class="loader" v-show="progress < 100">
      <h1>{{ progress }}%</h1>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  data () {
    return {
      progress: 0,
      cache: {}
    }
  },

  // Static manifest
  manifest: [
    './static/img/intro/test.jpg',
    './static/video/intro/bg.mp4',
    './static/video/intro/bg.webm',
    './static/video/home/scroll.mp4',
    './static/video/home/scroll.webm',
    './static/sound/home/sanik.mp3',
  ],

  ready: function() {
    let Loader = require('resource-loader');
    let loader = new Loader();

    let manifest = this.$options.manifest;

    manifest.forEach(function(file) {
        loader.add(file);
    });

    let that = this;
    loader.on('progress', function(event, resource){
      that.progress = Math.round(event.progress);
      console.log('progress', this.progress);
      let sequence = [];
      sequence = resource.url.split('/');
      sequence = sequence[sequence.length - 2];
      if(that.cache[sequence] === undefined) that.cache[sequence] = {};
      that.cache[sequence][resource.name] =
        {
          url: resource.url,
          data: resource.data
        }
      ;
    });

    loader.on('complete', function(event, resources){
      that.$broadcast('cache-loaded', 'that.cache');
      that.$route.router.go('/intro');
    });

    loader.load();
  }
}

这是我的 intro.vue

<template>
  <h1>INTRO</h1>
  <a v-link="{ path: '/home' }">Continue to Home</a>
</template>

<script>

export default {
  name: 'Intro',
  components: {},

  events: {
    'cache-loaded' : function(cached){
      console.log(this.$root.cache['intro']['./static/img/intro/test.jpg']);
      // ok it shows my element and what's next ??
    }
  }
}
</script>
<小时/>

编辑:我找到了不附加 HTML 原始元素的解决方案,而是使用 vue 的 native 数据绑定(bind)。

应用程序.vue

<template>
  <div class="">

    <div class="loader" v-show="progress < 100">
      <h1>{{ progress }}%</h1>
    </div>
    <router-view></router-view>

  </div>
</template>

<script>
export default {
  data () {
    return {
      progress: 0,
      img_cache: [],
      vid_cache: []
    }
  },

    // Static manifest
    manifest: [
      './static/intro/img/test.jpg',
      './static/intro/video/bg.mp4',
      './static/intro/video/bg.webm',
      './static/home/video/scroll.mp4',
      './static/home/video/scroll.webm'
    ],

    ready: function() {
      let Loader = require('resource-loader');
      let loader = new Loader();

      let that = this;
      let manifest = that.$options.manifest;

      manifest.forEach(function(file) {
          loader.add(file, file);
      });

      loader.on('progress', function(event, resource){
        that.progress = Math.round(event.progress);
        console.log('progress', this.progress);

        if(resource.url.match(/\.(jpe?g|png|gif|bmp)$/i)){
          that.img_cache.push({
            'name': resource.name,
            'src': resource.url
          });
        }else {
          that.vid_cache.push({
            'name': resource.name,
            'src': resource.url
          })
        }
      });

      loader.on('complete', function(event, resources){
        console.log('COMPLETE');
        that.$route.router.go('/intro');
      });

      loader.load();
    }
}
</script>

简介.vue

<template>
  <h1>INTRO</h1>
  <a v-link="{ path: '/home' }">Continue to Home</a>

  <div class="" v-for="itm in $root.vid_cache">
    <video v-bind:src="itm.src" autoplay loop>
    </video>
  </div>

</template>

<script>
import Loader from './Loader'

export default {
  name: 'Intro',
  components: {Loader},
  ready: function(){
    console.log('READY');
  }
}
</script>

我检查了网络检查器,资源仅加载一次。

最佳答案

我找到了不附加 HTML 原始元素的解决方案,而是使用 vue 的 native 数据绑定(bind)。

应用程序.vue

<template>
  <div class="">

    <div class="loader" v-show="progress < 100">
      <h1>{{ progress }}%</h1>
    </div>
    <router-view></router-view>

  </div>
</template>

<script>
export default {
  data () {
    return {
      progress: 0,
      img_cache: [],
      vid_cache: []
    }
  },

    // Static manifest
    manifest: [
      './static/intro/img/test.jpg',
      './static/intro/video/bg.mp4',
      './static/intro/video/bg.webm',
      './static/home/video/scroll.mp4',
      './static/home/video/scroll.webm'
    ],

    ready: function() {
      let Loader = require('resource-loader');
      let loader = new Loader();

      let that = this;
      let manifest = that.$options.manifest;

      manifest.forEach(function(file) {
          loader.add(file, file);
      });

      loader.on('progress', function(event, resource){
        that.progress = Math.round(event.progress);
        console.log('progress', this.progress);

        if(resource.url.match(/\.(jpe?g|png|gif|bmp)$/i)){
          that.img_cache.push({
            'name': resource.name,
            'src': resource.url
          });
        }else {
          that.vid_cache.push({
            'name': resource.name,
            'src': resource.url
          })
        }
      });

      loader.on('complete', function(event, resources){
        console.log('COMPLETE');
        that.$route.router.go('/intro');
      });

      loader.load();
    }
}
</script>

简介.vue

<template>
  <h1>INTRO</h1>
  <a v-link="{ path: '/home' }">Continue to Home</a>

  <div class="" v-for="itm in $root.vid_cache">
    <video v-bind:src="itm.src" autoplay loop>
    </video>
  </div>

</template>

<script>
import Loader from './Loader'

export default {
  name: 'Intro',
  components: {Loader},
  ready: function(){
    console.log('READY');
  }
}
</script>

我检查了网络检查器,资源仅加载一次。

关于javascript - vue.js 缓存视频并播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37575897/

相关文章:

java - 如何通过AJAX处理响应内容

javascript - 使用 jQuery 标记适当的 li/星期几

android - 无法使用node-xmpp客户端连接到GCM

javascript - Promise 控制流程 - catch 与 onRejected

ios - 如何在 UITableViewCell 中添加视频

javascript - 使用 NodeJs 处理文件系统的新手问题

javascript - 用定长多维数组填充Javascript对象

node.js - 带有 Mustache 模板、connect 和 node.js 的空白输出

javascript - HTML 视频播放器对于某些文件出现故障

html - 从页面中删除HTML5音频或视频时,如何防止播放?