vue.js缓存视频并播放它

时间:2016-06-01 18:08:39

标签: 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();
  }
}

这是我的介绍

<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的本机数据绑定。

App.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>

Intro.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>

我在网络检查员中检查过,资源只加载了一次。

1 个答案:

答案 0 :(得分:0)

我发现解决方案不附加HTML原始元素,而是使用vue的本机数据绑定。

App.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>

Intro.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>

我在网络检查员中检查过,资源只加载了一次。