标题js怎么判断视频链接是否能播放
这个函数用提供的URL创建一个新的Video元素,并根据视频是否可以播放来解析一个Promise。它监听canplaythrough事件来确定视频是否可播放,并监听error事件来处理可能发生的任何错误。
function canPlayVideo(url) {return new Promise((resolve, reject) => {const video = document.createElement('video');video.src = url;video.addEventListener('canplaythrough', () => {resolve(true);video.remove();});video.addEventListener('error', () => {resolve(false);video.remove();});document.body.appendChild(video);});
}// Example usage
const videoUrl = 'https://www.example.com/video.mp4';
canPlayVideo(videoUrl).then((result) => {if (result) {console.log('Video is playable');} else {console.log('Video is not playable');}});