使用jQuery播放/暫停 HTML5視訊
我嘗試用jQuery控制HTML5視訊,兩個視訊分別在兩個tab中,我希望點中tab後,該tab裡的視訊可以立即播放,而另外tab裡的視訊能夠停止。
我的程式碼是這樣的:
靜音和取消靜音“
下面是一個比較完整的例子:
我的程式碼是這樣的:
$(
'#playMovie1'
).click(function(){
$(
'#movie1'
).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
</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)"
><<</button>
<button id=
"play"
onclick=
"vidplay()"
>></button>
<button id=
"fastFwd"
onclick=
"skip(10)"
>>></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"
><<</button>
<button id=
"play"
title=
"Play
button"
>></button>
<button id=
"fwd"
title=
"Forward
button"
>>></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>
相關文章
- Video元件:控制視訊的播放與暫停IDE元件
- 3、Opencv播放視訊、儲存、暫停視訊,開啟攝像頭OpenCV
- video視訊標籤播放時js/jQuery顯示播放控制元件會導致播放暫停解決辦法IDEJSjQuery控制元件
- vue+樹莓派實現實時視訊播放暫停Vue樹莓派
- 多個audio播放器播放,暫停,時間控制播放器
- 使用SVG實現的一個Android播放/暫停按鈕SVGAndroid
- video影片標籤播放時js/jQuery顯示播放控制元件會導致播放暫停解決辦法IDEJSjQuery控制元件
- HTML5:網頁如何播放VR視訊?HTML網頁VR
- 短視訊直播原始碼,視訊計時效果,可隨時按鍵暫停原始碼
- html5播放mp4視訊程式碼HTML
- WebView播放HTML5視訊,黑屏的問題。WebViewHTML
- 通過video實現視訊暫停時出現圖片IDE
- 7款很棒的 HTML5 視訊播放器HTML播放器
- jQuery具有暫停功效果新聞列表向上滾動jQuery
- WebView 視訊全屏播放(基於Html5 video)WebViewHTMLIDE
- 簡單WebView使用播放視訊停止WebView
- iOS 視訊播放的簡單使用iOS
- 使用VideoView播放rtsp視訊流IDEView
- 【方法】Win10 暫停影片重新播放聲音突然變大Win10
- 【HTML5初探之多媒體元素】視訊播放HTML5、Flash誰才是王道?HTML
- 視音訊播放音訊
- opencv視訊播放OpenCV
- Dplayer Html5 彈幕視訊播放器的實現HTML播放器
- Facebook平臺視訊正式轉向HTML5播放器HTML播放器
- flutter實現B站播放器暫停時的header效果Flutter播放器Header
- iOS開發:音訊播放、錄音、視訊播放、拍照、視訊錄製iOS音訊
- windows 如何暫停更新Windows
- Android開發 海康威視 多路視訊播放(同時播放視訊)Android
- html5播放m3u8視訊,web端看直播HTMLWeb
- unity中播放視訊Unity
- iOS視訊播放(二)iOS
- 視訊播放學習
- 視訊播放器播放器
- html5視訊HTML
- HTML5本地視訊播放器設計(+彈幕功能)HTML播放器
- Android 音視訊開發 - 使用AudioTrack播放音訊Android音訊
- iOS開發系列--音訊播放、錄音、視訊播放、拍照、視訊錄製(轉)iOS音訊
- HTML5+CSS3+jQuery製作視訊播放器完全指南HTMLCSSS3jQuery播放器