廊坊小程序制作_JS完成预加载视频音频/视频获取

JS实现预加载视频音频/视频获取截图(返回canvas截图)       这篇文章主要介绍了JS实现预加载视频音频/视频获取截图(返回canvas截图)的相关资料,需要的朋友可以参考下
if (!opts.url || typeof opts.url !== 'string') { this.callback({code: 1, msg: CODES[1]}); return; // 创建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; // 非跨域资源 // !!非同域资源无法获取数据 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 // 数据转换 toBlobData: function (data, type) { // 获取base64数据 // base64数据格式: // "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 返回的默认格式是 image/png return new Blob([ia], {type: type}); * addEventListener 事件监听 * @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|htt凡科抠图):\/\//.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标签的属性,方法和事件汇总
// TF12138/
import loadMedia from '@/common/js/load-media'
let loadVideo = new loadMedia({
 type: 'video',
 url: '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) {
 }

总结

以上所述是小编给大家介绍的JS实现预加载视频音频/视频获取截图(返回canvas截图),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对凡科网站的支持!


相关阅读