Skip to content

vitepress ​

在微信中也能放视频 ​

js
<div style="display: flex; justify-content: center; align-items: center; margin: 20px 0; text-align: center;">
  <video 
    src="/myvedio/s001p003.mp4" 
    controls 
    preload="none"
    poster="/mypic/视频封面.jpg" 
    playsinline
    webkit-playsinline
    x5-playsinline
    x5-video-player-type="h5"
    x5-video-player-fullscreen="true"
    x5-video-orientation="portrait"
    style="width: 100%; max-width: 480px; height: auto; border-radius: 8px; background: #000;"
  >
    您的浏览器不支持 HTML video 标签。
  </video>
  
  
</div>

<!-- 微信浏览器兼容性脚本 -->
<script setup>
import { onMounted } from 'vue'

onMounted(() => {
  const video = document.querySelector('video');
  
  // 检测微信浏览器
  function isWeChatBrowser() {
    return /MicroMessenger/i.test(navigator.userAgent);
  }
  
  if (isWeChatBrowser() && video) {
    // 微信浏览器特殊处理
    video.setAttribute('x5-video-player-type', 'h5');
    video.setAttribute('x5-video-player-fullscreen', 'true');
    video.setAttribute('x5-video-orientation', 'portrait');
    
    // 强制视频在微信中内联播放
    video.setAttribute('playsinline', 'true');
    video.setAttribute('webkit-playsinline', 'true');
    video.setAttribute('x5-playsinline', 'true');
    
    // 添加点击事件处理
    video.addEventListener('click', function() {
      if (video.paused) {
        video.play().catch(function(e) {
          console.log('微信浏览器播放失败:', e);
          // 显示提示
          const tip = document.createElement('div');
          tip.style.cssText = 'position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: rgba(0,0,0,0.8); color: white; padding: 10px; border-radius: 5px; z-index: 9999;';
          tip.innerHTML = '请在微信中点击右上角,选择在浏览器中打开';
          document.body.appendChild(tip);
          setTimeout(function() {
            document.body.removeChild(tip);
          }, 3000);
        });
      } else {
        video.pause();
      }
    });
  }
})
</script>

不教话术只练心态的汤哥

微信:pp09pp90

不教话术只练心态的汤哥