How to play/pause video using jQuery ?

Method 1: Using trigger() method: The trigger() method is used to execute a specified event and the default behavior of the event. The event to be executed is passed as a parameter to this method.
The ‘play’ event is used to play any media element and similarly the ‘pause’ event is used to pause any media element. Using these events with the trigger() method will play or pause the video as required.

Syntax:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Play the video
$('#sample_video').trigger('play');
  
// Pause the video
$('#sample_video').trigger('pause');

chevron_right


Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to play/pause video
        using JQuery?
    </title>
      
    <script src=
    </script>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        Play/pause HTML 5 video
        using JQuery?
    </b>
      
    <p>
        Click on the buttons to play
        or pause the video.
    </p>
      
    <button onclick="playVideo()">
        Play Video
    </button>
      
    <button onclick="pauseVideo()">
        Pause Video
    </button>
    <br>
      
    <video id="sample_video" width="360" height="240">
      
        <source src=
                type="video/mp4">
    </video>
      
    <script>
        function playVideo() {
            $('#sample_video').trigger('play');
        }
        function pauseVideo() {
            $('#sample_video').trigger('pause');
        }
    </script>
</body>
  
</html>

chevron_right


Output:
play-pause

Method 2: Using the play() and pause() method: The play() method in JavaScript is used to attempt the playback of a media file. In jQuery, the video file is first selected using a selector and the actual element is selected using the get() method. Then the play() method is used on this element to attempt to start the video.



The pause() method in JavaScript is used to pause the playback of a media file. In jQuery, the video file is first selected using a selector and the actual element is selected using the get() method. The pause() method is then used on this element to pause the video.

Syntax:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Play the video
$('#sample_video').get(0).play();
  
// Pause the video
$('#sample_video').get(0).pause();

chevron_right


Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to play/pause video
        using JQuery?
    </title>
      
    <script src=
    </script>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        Play/pause HTML 5 video
        using JQuery?
    </b>
      
    <p>
        Click on the buttons to play
        or pause the video.
    </p>
      
    <button onclick="playVideo()">
        Play Video
    </button>
      
    <button onclick="pauseVideo()">
        Pause Video
    </button>
    <br>
      
    <video id="sample_video" width="360" height="240">
        <source src=
                type="video/mp4">
    </video>
      
    <script>
        function playVideo() {
            $('#sample_video').get(0).play();
        }
      
        function pauseVideo() {
            $('#sample_video').get(0).pause();
        }
    </script>
</body>
  
</html>

chevron_right


Output:
play-pause-2




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.