流媒体

通过BLOB加密视频文件

2018-11-15  本文已影响0人  ITgecko
前言
createObjectURL与BLOB
代码实现
const Koa = require('koa')
const Router = require('koa-router')
const buffer = require('buffer');
const app = new Koa()
const router = new Router()
const fs = require('fs')
const video = async (ctx, next) => {
  try {
    // open 一个放在服务器的视频
    let data = fs.readFileSync('XXX.XXX.XXX/simple.mp4')
    ctx.response.body = data
  } catch (e) {
    return Promise.reject({
      status: 500,
      message: '视频传输错误'
    })
  }
  next()
}

router.get('/video', video)

app.use(router.routes()).use(router.allowedMethods())
app.listen(3001)
    let xhr = new XMLHttpRequest()
    xhr.open('GET', 'http://localhost:3001/video', true)
    xhr.responseType = 'blob'
    xhr.onload = function(e) {
      if (this.status === 200) {
        // 获取blob对象
        let blob = this.response
        console.log(blob)
        // 获取blob对象地址,并把值赋给容器
        $("#sound").attr("src", URL.createObjectURL(blob));
      }
    }
    xhr.send()
上一篇下一篇

猜你喜欢

热点阅读