/    Sign up×
Community /Pin to ProfileBookmark

Manipulating video controls

Noob alert. I’m not a coder by trade. I’m just learning.

For the life of me, I can’t figure out how to manipulate the video controls on this page. The page loads a local video, just as intended, but I’d like to add button controls for play/pause, mute/unmute, etc. I believe once I get one control working then I can replicate the process for others. But I’m just not figuring out how to add the control. Any help will be greatly appreciated.

Thanks in advance,
Andrew

[CODE]<!DOCTYPE html>

<html lang=”en” xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta charset=”utf-8″ />
<title>Local File Player</title>

<!–SOURCE OF EXAMPLE: http://jsfiddle.net/dsbonev/cCCZ2/embedded/result,js,html,css/–>

<style>
video, input {
display: block;
}
input {
width: 100%;
}
.info {
background-color: aqua;
}
.error {
background-color: red;
color: white;
}
</style>
<!–
<script type=”text/javascript” src=”js/jquery.js”></script>
–>
<script type=”text/javascript”>
function init() { // Master function, encapsulates all functions
(function localFileVideoPlayer() {
‘use strict’
var URL = window.URL || window.webkitURL
var displayMessage = function (message, isError) {
var element = document.querySelector(‘#message’)
element.innerHTML = message
element.className = isError ? ‘error’ : ‘info’
}
var playSelectedFile = function (event) {
var file = this.files[0]
var type = file.type
var videoNode = document.querySelector(‘video’)
var canPlay = videoNode.canPlayType(type)
if (canPlay === ”) canPlay = ‘no’
var message = ‘Can play type “‘ + type + ‘”: ‘ + canPlay
var isError = canPlay === ‘no’
displayMessage(message, isError)

if (isError) {
return
}
var fileURL = URL.createObjectURL(file)
videoNode.src = fileURL
}
var inputNode = document.querySelector(‘input’)
inputNode.addEventListener(‘change’, playSelectedFile, false)
})()
}// end of master
function muteUnmute() {
alert(“need a working control”)
}
</script>
</head>

<body onload=”init();”>
<h1>HTML5 local video file player example</h1>
<div id=”message”></div>
<input type=”file” accept=”video/*” />
<video id=”video” controls autoplay></video>
<br />
<button onclick=”muteUnmute()” style=”cursor:pointer”>Mute/Unmute</button>
</body>
</html>[/CODE]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@SerenityNetworkauthorJun 20.2017 — Never mind. I figured it out.

[CODE]<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Local File Player</title>
<!--SOURCE: http://jsfiddle.net/dsbonev/cCCZ2/embedded/result,js,html,css/-->

<style>
video, input {
display: block;
}

input {
width: 100%;
}

.info {
background-color: aqua;
}

.error {
background-color: red;
color: white;
}
</style>

</head>

<body onload="init();">
<h1>HTML5 local video file player example</h1>
<div id="message"></div>
<input type="file" accept="video/*" />
<video id="video" controls autoplay></video>
<br />
<button onclick="muteUnmute()" style="cursor:pointer">Mute/Unmute</button>
<br />
<span class="basic">[Current Playback Speed: <span id="speed"></span>]&nbsp; &nbsp;</span>
<br />
<span class="basic">&nbsp; &nbsp;[Playback position: <span id="elapsed"></span> of <span id="duration"></span>]</span>
<br />


<script type="text/javascript">
var vid = document.getElementById("video");
function init() { // Master function, encapsulates all functions
(function localFileVideoPlayer() {
'use strict'
var URL = window.URL || window.webkitURL
var displayMessage = function (message, isError) {
var element = document.querySelector('#message')
element.innerHTML = message
element.className = isError ? 'error' : 'info'
}
var playSelectedFile = function (event) {
var file = this.files[0]
var type = file.type
var videoNode = document.querySelector('video')
var canPlay = videoNode.canPlayType(type)
if (canPlay === '') canPlay = 'no'
var message = 'Can play type "' + type + '": ' + canPlay
var isError = canPlay === 'no'
displayMessage(message, isError)

if (isError) {
return
}
var fileURL = URL.createObjectURL(file)
videoNode.src = fileURL
}
var inputNode = document.querySelector('input')
inputNode.addEventListener('change', playSelectedFile, false)
})()
}// end of master

// ------------ SOUND ------------
function muteUnmute() {
if (vid.muted)
vid.muted = false;
else
vid.muted = true;
}
}
</script>

</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@rootJun 21.2017 — A few things,

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ??? you only need <html>

Using an anon function is not good programming, especially when an error is present as most errors report an error at line 1 and not where the actual error is.

The basics of HTML5 video can be found on the W3Schools site, in it you will find the more appropriate way to construct your player.
Copy linkTweet thisAlerts:
@SerenityNetworkauthorJun 21.2017 — Thanks. I'm learning as I go.

I did look at W3Schools site. I found examples I have used for opening and playing a web-sourced video file, but I didn't find anything for opening and playing a local file.

Thanks again,

Andrew
Copy linkTweet thisAlerts:
@rootJun 22.2017 — You use relative paths so if the image or video is in the same directory, you just use the filename and ./foldername/filename.ext as another example of the source file being in another directory.
Copy linkTweet thisAlerts:
@SerenityNetworkauthorJun 22.2017 — You use relative paths so if the image or video is in the same directory, you just use the filename and ./foldername/filename.ext as another example of the source file being in another directory.[/QUOTE]

I don't see how relative paths would work in my situation. The web page is being served up from an online shared hosting service. The video files are on the user's PC. There is no relative path I can utilize.

I found [U][COLOR="#0000FF"]this[/COLOR][/U] script for viewing video files that can be referenced by a URL or where the files can be referenced by a relative path (because they are hosted in the same directory path as the HTML page). It works fine for those situations, but I haven't figured out any way to have it load video files that are on a user's local machine. If it could be modified to allow viewing of local files that would be great. But if not, or until then, I'm stuck with using the script shown in the earlier post.

Thanks again,

Andrew
Copy linkTweet thisAlerts:
@rootJun 23.2017 — Your paths to the files should always be relative and only absolute if the media is on a different domain.

So if you have a page called player.html and you have a video file that is in the same folder as the player.html file, the src address would be just the file name because its path relative to the document requires no path setting. If it was a vidseo in a folder called camera then you would use something like src="./camera/filename.ext" to reference the file
×

Success!

Help @SerenityNetwork spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...