【轉】使用jQuery播放/暂停 HTML5视频

jQuery不可以使用play()方法,但js是可以的:

document.getElementById(‘movie1‘).play();

解决方法:
play并不是jQuery的函数,而是DOM元素的函数,所以我们需要通过DOM来调用play,代码如下:

$(‘#videoId‘).get(0).play();

最简单的方法实现Play和Pause:

$(‘video‘).trigger(‘play‘);

$(‘video‘).trigger(‘pause‘)

点击视频就能播放和暂停

$("video").trigger("play");//for auto play

$("video").addClass(‘pause‘);//for check pause or play add a class

$(‘video‘).click(function() {

  if ($(this).hasClass(‘pause‘)) {

    $("video").trigger("play");

    $(this).removeClass(‘pause‘);

    $(this).addClass(‘play‘);

  } else {

    $("video").trigger("pause");

    $(this).removeClass(‘play‘);

    $(this).addClass(‘pause‘);

  }

})

静音和取消静音

$(‘body‘).find("video").attr(‘id‘‘video‘)

var myVid = document.getElementById("video");

$(‘.sound-icon‘).click(function() {

  //here "sound-icon" is a anchor class.

  var sta = myVid.muted;

  if (sta == true) {

      myVid.muted = false;

  } else {

  myVid.muted = true;

  }

})

HTML 5中播放视频的方法:

<video width="640" height="360" src="http://www.youtube.com/demo/google_main.mp4" controls autobuffer>

<p> Try this page in Safari 4! Or you can

<a href="http://www.youtube.com/demo/google_main.mp4">download the video</a> instead.</p>

</video>

自动播放:

<video src="abc.mov" autoplay></video>

使用poster在视频无法加载时显示图片:

<video width="640" height="360" src="http://www.youtube.com/demo/google_main.mp" autobuffer controls poster="whale.png">

<p>Try this page in Safari 4! Or you can <a href="http://www.youtube.com/demo/google_main.mp4">download the video</a> instead.</p>

</video>

一个比较简洁的例子:

<script type="text/javascript">

  function vidplay() {

    var video = document.getElementById("Video1");

    var button = document.getElementById("play");

    if (video.paused) {

      video.play();

      button.textContent = "||";

    } else {

      video.pause();

      button.textContent = ">";

    }

  }

  function restart() {

    var video = document.getElementById("Video1");

    video.currentTime = 0;

  }

  function skip(value) {

    var video = document.getElementById("Video1");

    video.currentTime += value;

  }

</script>

</head>

<body>

<video id="Video1" >

// Replace these with your own video files.

  <source src="demo.mp4" type="video/mp4" />

  <source src="demo.ogv" type="video/ogg" />

  HTML5 Video is required for this example.

  <a href="demo.mp4">Download the video</a> file.

</video>

<div id="buttonbar">

<button id="restart" onclick="restart();">[]</button>

<button id="rew" onclick="skip(-10)">&lt;&lt;</button>

<button id="play" onclick="vidplay()">&gt;</button>

<button id="fastFwd" onclick="skip(10)">&gt;&gt;</button>

</div>

下面是一个比较完整的例子:

<html >

<head>

<title>Full player example</title>

<!-- Uncomment the following meta tag if you have issues rendering this page on an intranet or local site. -->

<!-- <meta http-equiv="X-UA-Compatible" content="IE=edge"/> -->

<script type="text/javascript">

  function init() { // Master function, encapsulates all functions

    var video = document.getElementById("Video1");

    if (video.canPlayType) { // tests that we have HTML5 video support

    // if successful, display buttons and set up events

      document.getElementById("buttonbar").style.display = "block";

      document.getElementById("inputField").style.display = "block";

      // helper functions

      // play video

      function vidplay(evt) {

        if (video.src == "") { // inital source load

        getVideo();

      }

      button = evt.target; // get the button id to swap the text based on the state

      if (video.paused) { // play the file, and display pause symbol

        video.play();

        button.textContent = "||";

      } else // pause the file, and display play symbol

        video.pause();

        button.textContent = ">";

      }

    }

    // load video file from input field

    function getVideo() {

      var fileURL = document.getElementById("videoFile").value; // get input field

      if (fileURL != "") {

        video.src = fileURL;

        video.load(); // if HTML source element is used

        document.getElementById("play").click(); // start play

      } else {

        errMessage("Enter a valid video URL"); // fail silently

      }

    }

 

// button helper functions

// skip forward, backward, or restart

function setTime(tValue) {

// if no video is loaded, this throws an exception

try {

if (tValue == 0) {

video.currentTime = tValue;

}

else {

video.currentTime += tValue;

}

catch (err) {

// errMessage(err) // show exception

errMessage("Video content might not be loaded");

}

}

// display an error message

function errMessage(msg) {

// displays an error message for 5 seconds then clears it

document.getElementById("errorMsg").textContent = msg;

setTimeout("document.getElementById(‘errorMsg‘).textContent=‘‘"5000);

}

// change volume based on incoming value

function setVol(value) {

var vol = video.volume;

vol += value;

// test for range 0 - 1 to avoid exceptions

if (vol >= 0 && vol <= 1) {

// if valid value, use it

video.volume = vol;

else {

// otherwise substitute a 0 or 1

video.volume = (vol < 0) ? 0 1;

}

}

// button events

// Play

document.getElementById("play").addEventListener("click", vidplay, false);

// Restart

document.getElementById("restart").addEventListener("click", function () {

setTime(0);

}, false);

// Skip backward 10 seconds

document.getElementById("rew").addEventListener("click", function () {

setTime(-10);

}, false);

// Skip forward 10 seconds

document.getElementById("fwd").addEventListener("click", function () {

setTime(10);

}, false);

// set src == latest video file URL

document.getElementById("loadVideo").addEventListener("click", getVideo, false);

// fail with message

video.addEventListener("error", function (err) {

errMessage(err);

}, true);

// volume buttons

document.getElementById("volDn").addEventListener("click", function () {

setVol(-.1); // down by 10%

}, false);

document.getElementById("volUp").addEventListener("click", function () {

setVol(.1); // up by 10%

}, false);

// playback speed buttons

document.getElementById("slower").addEventListener("click", function () {

video.playbackRate -= .25;

}, false);

document.getElementById("faster").addEventListener("click", function () {

video.playbackRate += .25;

}, false);

document.getElementById("normal").addEventListener("click", function () {

video.playbackRate = 1;

}, false);

document.getElementById("mute").addEventListener("click", function (evt) {

if (video.muted) {

video.muted = false;

evt.target.innerHTML = "<img alt=‘volume on button‘ src=‘vol2.png‘ />"

else {

video.muted = true;

evt.target.innerHTML = "<img alt=‘volume off button‘ src=‘mute2.png‘ />"

}

}, false);

// end of runtime

}// end of master

</script>

</head>

<body onload="init();" >

<video id="Video1" controls style="border: 1px solid blue;" height="240" width="320" title="video element">

HTML5 Video is required for this example

</video>

<div id="buttonbar" style="display: none;")>

<button id="restart" title="Restart button">[]</button>

<button id="slower" title="Slower playback button">-</button>

<button id="rew" title="Rewind button" >&lt;&lt;</button>

<button id="play" title="Play button">&gt;</button>

<button id="fwd" title="Forward button" >&gt;&gt;</button>

<button id="faster" title="Faster playback button">+</button>

<button id="Button2" title="Mute button" ><img alt="Volume on button" src="vol2.png" /></button>

<br />

<label>Playback </label>

<label>Reset playback rate: </label><button id="normal" title="Reset playback rate button">=</button>

<label> Volume </label>

<button id="volDn" title="Volume down button">-</button>

<button id="volUp" title="Volume up button">+</button>

<button id="mute" title="Mute button" ><img alt="Volume on button" src="vol2.png" /></button>

</div>

<br/>

<div id= "inputField" style="display:none;" >

<label>Type or paste a video URL: <br/>

<input type="text" id="videoFile" style="width: 300px;" title="video file input field" value="http://ie.microsoft.com/testdrive/ieblog/2011/nov/pp4_blog_demo.mp4" /> 

<button id="loadVideo" title="Load video button" >Load</button>

</label>

</div>

<div title="Error message area" id="errorMsg" style="color:Red;"></div>

</body>

</html>

原文鏈接:http://blog.okbase.net/jquery2000/archive/4485.html

时间: 2025-01-13 03:18:40

【轉】使用jQuery播放/暂停 HTML5视频的相关文章

使用jQuery播放/暂停 HTML5视频

文章来自:http://blog.okbase.net/jquery2000/archive/4485.html 我尝试用jQuery控制HTML5视频,两个视频分别在两个tab中,我希望点中tab后,该tab里的视频可以立即播放,而另外tab里的视频能够停止.我的代码是这样的: 1 2 3 $('#playMovie1').click(function(){ $('#movie1').play(); }); 但发现这样不行,而用以下的js是可以的: 1 document.getElementB

HTML5视频、音频播放事件属性与API控件

1.video.audio标签 <video src = "movie.mp4" controls = "controls"><video> 或者<video controls = "controls"> <source src = "movie.mp4"> <video>2.HTML5视频API 控件 (1)获得video标签 ①通过DOM对象 var video

[HTML5] Video 标签播放及控制视频

Video 是 HTML5 中新增的标签,使用 Video 标签可以播放 ogg.mp4 .webm 等格式的视频,Video 标签的引入,减少前端页面对 Flash 动画的依赖. 0x0 预备知识 不同的浏览器支持的视频格式可能不同,以下是摘自 W3School 各个浏览器对三种视频格式的支持情况: 0x1 在 Video 中嵌入视频 <video src="/movie.mp4" controls="controls"> 你就用这个浏览器有出息吗?

【转】Android 4.4中播放HTML5视频&lt;video&gt;的Bug

近期Nexus 4手机自动升级到Android4.4,本来挺好的一件事儿,结果发现自己的应用中出现一个Bug,应用中使用了Webview播放HTML5视频,代码如下: <video width="480" height="280" poster="test.jpg" src="test.mp4" preload="auto"></video> 在4.3版本之前播放正常,新版本中播放时

Html5之高级-4 HTML5视频处理(H5中播放视频、编程实现视频播放器)

一.在 HTML5 中播放视频 在 HTML5 中播放视频 - HTML5 中提到最多的是视频处理,视频也是互联网中的重要应用.在HTML5中,增加了一个元素,以便在HTML文档中插入和播放视频,这个元素就是video元素 - video元素提供了播放.暂停和音量控件来控制视频,也提供了width和height属性控制视频的尺寸.如设置的高度和宽度等. - 使用video元素至少要提供两种视频格式的文件: OGG和MP4 - OGG: 包含Theora 视频和 Vorbis 音频解码器 - MP

[转] Android 4.4中播放HTML5视频&lt;video&gt;的Bug

近期Nexus 4手机自动升级到Android4.4,本来挺好的一件事儿,结果发现自己的应用中出现一个Bug,应用中使用了Webview播放HTML5视频,代码如下: <video width="480" height="280" poster="test.jpg" src="test.mp4" preload="auto"></video> 在4.3版本之前播放正常,新版本中播放时

HTML5播放暂停音乐

查看效果:http://hovertree.com/code/jquery/ueyf7gn4.htm 代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>jQuery播放音乐 - 何问起</title><base tar

HTML5-video(播放暂停视频;打开关闭声音;进度条)

<!DOCTYPE html> <html> <head> <title>HTML5-video(播放暂停视频:打开关闭声音:进度条)</title> <meta charset="utf-8"/> </head> <body> <video id="video1" controls="controls" width="400px&qu

HTML5下的Audio标签控件的歌曲播放暂停演示

1 <!-- 使用了HTML5的标签<audio> 图片播放按钮增加了监听监听事件,控制音乐的播放暂停 2 使用了document的几个重要函数: 3 1.createElement创建标签对象, 4 2.getElementById获取对象以及img对象的src属性 5 3.document.body添加控件appendChild()函数 6 了解document的强大.可以创建对象,操作body标签等--> 7 <!DOCTYPE html> 8 <html&