简体   繁体   English

带有Javascript的HTML5视频播放/停止按钮

[英]HTML5 Video Play/Stop button with Javascript

My custom HTML5 video play is almost complete, I'm just having a slight issue with the Stop button which essentially is a pause button that just resets the video to the beginning. 我自定义的HTML5视频播放已接近完成,“停止”按钮只是一个小问题,它本质上是一个暂停按钮,只是将视频重置为开始。

Below is my code (albeit a little messy); 下面是我的代码(尽管有些混乱);

<script type="text/javascript">
    function stopPlayer() {
        var mediaPlayer;

        mediaPlayer = document.getElementById('media-video');
        mediaPlayer.controls = false;   

        mediaPlayer.pause();
        mediaPlayer.currentTime = 0;

        if ( mediaPlayer.pause == true ) {
            $('.pause-btn').hide();
            $('.play-btn').show();
        }

    }

    function playPlayer() {
        var mediaPlayer;

        mediaPlayer = document.getElementById('media-video');
        mediaPlayer.controls = false;   

        mediaPlayer.play();
    }

    function playPause() {
        var mediaPlayer = document.getElementById('media-video');
        if (mediaPlayer.paused) {
            mediaPlayer.play(); 
            $('.pause-btn').show();
            $('.play-btn').hide();
        } else {
            mediaPlayer.pause(); 
            $('.play-btn').show();
            $('.pause-btn').hide();
        }

    }

</script>

The code in question is inside the stopPlayer(); 有问题的代码在stopPlayer()内部; function - 功能-

if ( mediaPlayer.pause == true ) {
            $('.pause-btn').hide();
            $('.play-btn').show();
        }

What I'm trying to do is check to see if the video is "Stopped" with the StopPlayer function and then hide the pause button and show the play button. 我想要做的是使用StopPlayer功能检查视频是否“已停止”,然后隐藏暂停按钮并显示播放按钮。 As these need to be reset when the the video is stopped. 由于在视频停止时需要重置这些设置。

Currently if you press the stop button, the pause button is still there. 当前,如果您按停止按钮,则暂停按钮仍然存在。

Any advice would be great :) 任何建议将是巨大的:)

Edit: Fixed @MelanciaUK suggested I removed the == true on the if statement, which fixed my issue. 编辑:固定@MelanciaUK建议我删除if语句上的== true,从而解决了我的问题。 Thanks! 谢谢!

Maybe it's just a little conditional/property mistake. 也许这只是一个条件错误/财产错误。

Try to replace: 尝试更换:

mediaPlayer.pause == true

With: 附:

mediaPlayer.paused

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM