国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > JS > 正文

JS實(shí)現(xiàn)預(yù)加載視頻音頻/視頻獲取截圖(返回canvas截圖)

2024-05-06 16:40:12
字體:
供稿:網(wǎng)友

 #load-media.js

/** * Create by Capricorncd 2017 */// 同域資源實(shí)現(xiàn)視頻截圖,可上傳的圖片數(shù)據(jù)格式// 非同域資源實(shí)現(xiàn)canvas截圖預(yù)覽// 提示碼const CODES = { 0: 'success', 1: 'The url is not valid', 2: 'onerror'}/** * constructor * @param opts.url 音頻|視頻URL * @param opts.type 'audio|video' * @param opts.callback 回調(diào)函數(shù) */function loadMedia(opts) { this.callback = opts.callback || function (res) {  console.log(res); } // 初始化 this.init(opts);}// prototypeloadMedia.prototype = { /**  * 初始化media  * @param url  */ init: function (opts) {  let self = this;  if (!opts.url || typeof opts.url !== 'string') {   this.callback({code: 1, msg: CODES[1]});   return;  }  // 創(chuàng)建media  let mediaType = opts.type === 'audio' ? 'audio' : 'video';  this.media = document.createElement(mediaType);  console.log('this.media', this.media);  // loaded  this.listener('canplaythrough', function (e) {   // 截圖   if (mediaType === 'video') {    self.screenshot();   } else {    self.callback({     code: 0,     msg: CODES[0],     thumb: null,     media: this.media,     canvas: null    });   }  });  // error  this.listener('error', function (e) {   self.callback({code: 2, msg: CODES[2], data: e});  })  this.media.setAttribute('src', opts.url); }, screenshot: function () {  // create canvas  let canvas = document.createElement('canvas');    canvas.width = this.media.videoWidth;    canvas.height = this.media.videoHeight;  let ctx = canvas.getContext('2d');  // 截取  ctx.drawImage(this.media, 0, 0);  let thumb = null;  // 非跨域資源  // !!非同域資源無法獲取數(shù)據(jù)  try {   let type = 'image/png';   let data = canvas.toDataURL(type);   thumb = this.toBlobData(data, type);  } catch (e) {}  this.callback({   code: 0,   msg: CODES[0],   thumb: thumb,   media: this.media,   canvas: canvas  }) }, // 數(shù)據(jù)轉(zhuǎn)換 toBlobData: function (data, type) {  // 獲取base64數(shù)據(jù)  // base64數(shù)據(jù)格式:  // "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAkGB+wgHBgkIBwgKCgkLDRYPDQw//9k="  data = window.atob(data.split(',')[1]);  let ia = new Uint8Array(data.length);  for (let i = 0; i < data.length; i++) {   ia[i] = data.charCodeAt(i);  };  // canvas.toDataURL 返回的默認(rèn)格式是 image/png  return new Blob([ia], {type: type}); }, /**  * addEventListener 事件監(jiān)聽  * @param en EventName  * @param callback  */ listener: function (en, callback) {  this.media.addEventListener(en, function (e) {   if (callback) {    callback(e);   } else {    console.warn(this);   }  }, false); } /**  * 資源是否跨域  * @param url 資源地址  * @returns {boolean}  */ // isCrossDomain: function (url) { //  let loc, host, protocol; //  loc = window.location; //  host = loc.host; //  protocol = loc.protocol; //  // 是否為http鏈接 //  if (/(http|https)://///.test(url)) { //   if (url.indexOf(protocol + '//' + host) >= 0) { //    return false; //   } else { //    return true; //   } //  } //  // './xxx.mp4' '/xxx.mp4' 'xxx.mp4' //  return false; // }}

export default loadMedia;

// 參考資料// HTML5的Video標(biāo)簽的屬性,方法和事件匯總// http://www.cnblogs.com/TF12138/p/4448108.html# 使用import loadMedia from '@/common/js/load-media'let loadVideo = new loadMedia({ type: 'video', url: 'http://xmqvip1-1253933147.file.myqcloud.com/chat/video/60/2017/09/29/qgj1c8K7oaYn-SCVideo-Merged.mp4', callback: handleCallback})function handleCallback (res) {    console.log(res)    // canplaythrough    if (res.code === 0) {    }    // error    if (res.code === 2) {    }   }

總結(jié)

以上所述是小編給大家介紹的JS實(shí)現(xiàn)預(yù)加載視頻音頻/視頻獲取截圖(返回canvas截圖),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對VeVb武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 镇巴县| 鸡东县| 兴化市| 天水市| 博爱县| 福安市| 沐川县| 呼伦贝尔市| 贺州市| 平山县| 衡南县| 区。| 晋江市| 松阳县| 厦门市| 仙游县| 鸡泽县| 西乌珠穆沁旗| 萨嘎县| 若羌县| 北海市| 通榆县| 昆明市| 文成县| 承德市| 营山县| 张掖市| 阿图什市| 乐都县| 四子王旗| 昆山市| 凤阳县| 来凤县| 江津市| 郑州市| 凤凰县| 务川| 江都市| 徐州市| 巴塘县| 察哈|