代码如图
getVideoBase64(url) {
const that = this
var dataURL = ''
var video = document.createElement('video')
video.setAttribute('crossOrigin', 'anonymous') // 处理跨域
video.setAttribute('src', url)
video.setAttribute('preload', 'auto')
video.play()
video.addEventListener('loadeddata', function () {
var canvas = document.createElement('canvas')
var width = video.videoWidth // canvas的尺寸和图片一样
var height = video.videoHeight
canvas.width = width
canvas.height = height
canvas.getContext('2d').drawImage(video, 0, 0, width, height) // 绘制canvas
dataURL = canvas.toDataURL('image/jpeg') // 转换为base64
that.firstImageBase = dataURL
that.files.name =
that.$moment(new Date()).format('YYYY/MM/DD/HH:mm:ss') + '.jpg'
that.files.type = 'image/jpeg'
that.postImg() // 这个函数是我上传图片给后台的接口
})
原本这就该是可以画出来的,安卓上测试是可以的,但是苹果没有反应,于是我一行一打印测试出来苹果真机loadeddata不触发,开发者工具和浏览器
模拟都没有问题,但是真机不进入loadeddata,于是我想了一个这种的方法,先判断机型是不是ios,如果是ios,调用一个回调函数,3秒后执行,差不
多也准备好了
const that = this
var dataURL = ''
var video = document.createElement('video')
video.setAttribute('crossOrigin', 'anonymous') // 处理跨域
video.setAttribute('src', url)
video.setAttribute('preload', 'auto')
video.play()
function fn3(callback) {
var i = 0
var timer = window.setInterval(() => {
i++
if (i == 1) {
clearInterval(timer)
if (callback) {
callback()
}
}
}, 1000)
}
if (/iPad|iPhone|iPod/.test(navigator.userAgent)) {
fn3(() => {
var width = video.videoWidth // canvas的尺寸和图片一样
var height = video.videoHeight
var canvas = document.createElement('canvas')
// that.alert(width, height)
canvas.width = width
canvas.height = height
canvas.getContext('2d').drawImage(video, 0, 0, width, height) // 绘制canvas
try {
dataURL = canvas.toDataURL('image/jpeg') // 转换为base64
} catch (err) {
console.log(err)
}
// dataURL = canvas.toDataURL('image/jpeg') // 转换为base64
that.alert(dataURL)
console.log(dataURL)
that.firstImageBase = dataURL
that.files.name =
that.$moment(new Date()).format('YYYY/MM/DD/HH:mm:ss') + '.jpg'
that.files.type = 'image/jpeg'
that.postImg()
})
倒是进去了,但是画出来的图是一张黑色的图,所以又打印dataURL,然后发现开发工具和浏览器都可以出图,但是真机弹出 data;
想问下这个怎么解决?求大神指教
请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
是微信上 无法自动播放 导致的 ,想解决 就先解决自动播放问题
同问