shellycooper 2019-04-29 17:51 采纳率: 0%
浏览 307

如何在使用flvplayback加载视频的情况下显示播放时间,或者使用loadMovie读取加载视频的播放时长

如何在使用flvplayback加载视频的情况下显示播放时间,或者使用loadMovie读取加载视频的播放时长

  • 写回答

1条回答 默认 最新

  • 一只小爪磕 新星创作者: 前端开发技术领域 2024-05-28 14:10
    关注

    本答案由爪磕协同GPT-4o回答,希望对您有所帮助 解决问题望采纳!


    在 HTML5 环境中加载视频并显示播放时间,可以使用 HTML5 <video> 元素结合 JavaScript 来实现。以下是详细步骤和示例代码来帮助你实现这个功能。

    使用 <video> 元素和 JavaScript

    首先,确保你有一个 HTML5 <video> 元素用于播放视频。然后,通过 JavaScript 获取视频的播放时间和总时长。

    HTML 结构

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Video Playback Time</title>
        <style>
            #video-container {
                width: 640px;
                margin: 0 auto;
            }
            #time-display {
                text-align: center;
                margin-top: 10px;
            }
        </style>
    </head>
    <body>
        <div id="video-container">
            <video id="myVideo" width="640" height="360" controls>
                <source src="path/to/your/video.mp4" type="video/mp4">
                Your browser does not support the video tag.
            </video>
            <div id="time-display">
                Current Time: <span id="currentTime">0:00</span> / Duration: <span id="duration">0:00</span>
            </div>
        </div>
    
        <script>
            document.addEventListener('DOMContentLoaded', (event) => {
                const video = document.getElementById('myVideo');
                const currentTimeDisplay = document.getElementById('currentTime');
                const durationDisplay = document.getElementById('duration');
    
                // Update duration once the video metadata is loaded
                video.addEventListener('loadedmetadata', () => {
                    const duration = formatTime(video.duration);
                    durationDisplay.textContent = duration;
                });
    
                // Update current time while the video is playing
                video.addEventListener('timeupdate', () => {
                    const currentTime = formatTime(video.currentTime);
                    currentTimeDisplay.textContent = currentTime;
                });
    
                // Function to format time in MM:SS
                function formatTime(seconds) {
                    const minutes = Math.floor(seconds / 60);
                    const secondsLeft = Math.floor(seconds % 60);
                    return `${minutes}:${secondsLeft < 10 ? '0' : ''}${secondsLeft}`;
                }
            });
        </script>
    </body>
    </html>
    

    解释

    1. HTML结构

      • <video> 元素用于加载和播放视频。
      • #time-display 是一个容器,用于显示当前播放时间和视频总时长。
    2. JavaScript事件监听

      • DOMContentLoaded 确保脚本在页面加载完成后运行。
      • loadedmetadata 事件在视频元数据(包括时长)加载完毕后触发,用于显示视频的总时长。
      • timeupdate 事件在视频播放过程中不断触发,用于更新当前播放时间。
    3. 格式化时间

      • formatTime 函数将时间从秒转换为“MM:SS”格式,方便用户阅读。

    兼容性与注意事项

    • 确保视频文件在服务器上正确配置,并且路径正确。
    • 使用 <source> 标签提供多种格式的视频文件,以确保跨浏览器兼容性(如 MP4 和 WebM)。
    • 如果需要支持自定义播放控件,可以隐藏默认控件并创建自己的控制界面。

    以上方法使用了现代浏览器支持的 HTML5 和 JavaScript 技术,适用于大多数主流浏览器。希望这些信息能帮助你在使用 HTML5 <video> 元素加载和显示视频播放时间。

    评论

报告相同问题?