Skip to content

Commit

Permalink
Added notes and solution for Project 11 - Custom HTML5 Video Player
Browse files Browse the repository at this point in the history
  • Loading branch information
lisaychuang committed Apr 30, 2018
1 parent 9525912 commit e1ab708
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 13 deletions.
31 changes: 18 additions & 13 deletions 11 - Custom Video Player/index-START.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>HTML Video Player</title>
<link rel="stylesheet" href="style.css">
</head>

<body>

<div class="player">
<video class="player__video viewer" src="https://player.vimeo.com/external/194837908.sd.mp4?s=c350076905b78c67f74d7ee39fdb4fef01d12420&profile_id=164"></video>
<div class="player">
<video class="player__video viewer" src="https://player.vimeo.com/external/194837908.sd.mp4?s=c350076905b78c67f74d7ee39fdb4fef01d12420&profile_id=164"></video>

<div class="player__controls">
<div class="progress">
<div class="player__controls">
<div class="progress">
<div class="progress__filled"></div>
</div>
<button class="player__button toggle" title="Toggle Play"></button>
<input type="range" name="volume" class="player__slider" min="0" max="1" step="0.05" value="1">
<input type="range" name="playbackRate" class="player__slider" min="0.5" max="2" step="0.1" value="1">
<button data-skip="-10" class="player__button">« 10s</button>
<button data-skip="25" class="player__button">25s »</button>
</div>
</div>
</div>
<button class="player__button toggle" title="Toggle Play"></button>
<input type="range" name="volume" class="player__slider" min="0" max="1" step="0.05" value="1">
<input type="range" name="playbackRate" class="player__slider" min="0.5" max="2" step="0.1" value="1">
<button data-skip="-10" class="player__button">« 10s</button>
<button data-skip="25" class="player__button">25s »</button>
<!-- Add fullscreen button -->
<button class="player__button fullscreen"></button>
</div>
</div>

<script src="scripts.js"></script>
</body>
</html>

</html>
111 changes: 111 additions & 0 deletions 11 - Custom Video Player/scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*** GET DOM ELEMENTS ***/

const player = document.querySelector('.player');
const video = player.querySelector('.viewer');

const progress = player.querySelector('.player__controls .progress');
const progressBar = player.querySelector('.player__controls .progress__filled');

const toggleButton = player.querySelector('.player__controls .toggle');
const ranges = player.querySelectorAll('.player__controls .player__slider');
const skipButtons = player.querySelectorAll('[data-skip]');

const fullScreen = player.querySelector('.player__controls .fullscreen');

// set default progress bar to 0% when video first load
progressBar.style.flexBasis = '0%';


/*** BUILD VIDEO PLAYER FUNCTIONS ***/

// Start or pause video when PLAY button is clicked
function togglePlay(){
video.paused ? video.play() : video.pause();
};

// Update play button symbol
function updateButton() {
const icon = this.paused ? '►' : '❚ ❚';
toggleButton.textContent = icon;
};

// Video skip forward
function skip(){
let skipTime = this.dataset.skip;
video.currentTime += parseFloat(skipTime);
};

// Update video volume or playback rate
function handleRangeUpdate(){
video[this.name] = this.value;
}

// function handleProgress() in Wes' video
// Update progress bar as video plays
function showProgress(){
const percentPlayed = (video.currentTime / video.duration) * 100;
progressBar.style.flexBasis = `${percentPlayed}%`;
}

// function scrub() in Wes' video
// By clicking on progress bar, start video at clicked position
function setVideoStart(e){
const scrubTime = (e.offsetX / progress.offsetWidth) * video.duration;
video.currentTime = scrubTime;
}

// Check if user is clicking down on mouse
let mousedown = false; // set a mousedown flag to check if mouse is clicked

function trackMouseClick() {
mousedown = !mousedown;
}

// STRETCH GOAL: Toggle fullscreen when user click on button
let fullscreen = false;

function toggleFullScreen(e){
if (fullscreen || e.keyCode === 27) { //if ESCAPE key pressed
player.style = 'max-width: 750px;';
document.querySelector('body').style = 'background: #7A419B;';
fullscreen = false;
} else {
player.style = 'max-width: none; width: 100%; height:100%; border: 0; padding: 0; overflow: hidden;';
document.querySelector('body').style = 'background: #000;';
fullscreen = true;
}
}

/*** HOOK UP EVENT LISTENERS ***/

// Play or pause video on mouse click
video.addEventListener('click', togglePlay);
toggleButton.addEventListener('click', togglePlay);

// Update toggleButton on video play or pause
video.addEventListener('play', updateButton);
video.addEventListener('pause', updateButton);

// Add event listener to SKIP buttons
skipButtons.forEach(btn => btn.addEventListener('click', skip))

// Add event listener to RANGE buttons
ranges.forEach(range => range.addEventListener('change', handleRangeUpdate));
ranges.forEach(range => range.addEventListener('mousemove', handleRangeUpdate));

// Add event listener to update video progress bar
video.addEventListener('timeupdate', showProgress);

// Add event listener to update video startTime based on user's click on progress bar
progress.addEventListener('click', setVideoStart);

// Add event listener to check if user is click the mouse
progress.addEventListener('mousedown', trackMouseClick);
progress.addEventListener('mouseup', trackMouseClick);

// Add event listener, if user mouse clicks and DRAG progress bar, update video start time
progress.addEventListener('mousemove', (e) => mousedown && setVideoStart(e));

/*** STRETCH GOAL: MAKE FULL SCREEN BUTTON WORK ***/
fullScreen.addEventListener('click', toggleFullScreen);
window.addEventListener('keyup', toggleFullScreen);
30 changes: 30 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ The `*` may be replaced by any name following the production rule of xml names w
* the name must not contain any semicolon (U+003A);
* the name must not contain capital A to Z letters.

Data attribute values can be accessible via:
`HTMLElement.dataset.attributeName` OR
`HTMLElement.dataset[attributeName]` OR
`HTMLElement[data-key="attributeName"]`

### [Document vs Window objects](http://eligeske.com/jquery/what-is-the-difference-between-document-and-window-objects-2/)

* `Window` is the first thing that gets loaded into the browser
Expand Down Expand Up @@ -236,6 +241,9 @@ An element can be both a `flex container` and a `flex item`!
#### `flex`
The `flex` CSS property specifies how a flex item will grow or shrink so as to fit the space available in its flex container. This is a shorthand property that sets `flex-grow`, `flex-shrink`, and `flex-basis`.

#### [`flex-basis`](https://css-tricks.com/almanac/properties/f/flex-basis/)
Specifies the initial size of the flex item, before any available space is distributed according to the flex factors.

## Project 6: Ajax Type Ahead Search Form

### [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
Expand Down Expand Up @@ -620,3 +628,25 @@ Other related properties:
* `MouseEvent.clientX`: The X coordinate of the mouse pointer in local (DOM content) coordinates.
* `MouseEvent.clientY`: The Y coordinate of the mouse pointer in local (DOM content) coordinates.

## Project 11: Custom HTML5 Video Player

### [HTMLMediaElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement)

The `HTMLMediaElement` interface adds to `HTMLElement` the properties and methods needed to support basic media-related capabilities that are common to audio and video. The `HTMLVideoElement <video>` and `HTMLAudioElement <audio>` elements both inherit this interface.

**Properties covered in this project:**
* `HTMLMediaElement.paused`: Returns a `Boolean` that indicates whether the media element is paused.
* `HTMLMediaElement.currentTime`: Gives the current playback time in seconds. Setting this value sets the media to the new playback time.

**Methods covered in this project:**
* `HTMLMediaElement.play()`: Begins playback of media
* `HTMLMediaElement.pause()`: Pauses media playback
* `HTMLMediaElement.duration`: gives the length of the media in seconds, or zero if no media data is available.

### [HTML Video Element: <video>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video)

The HTML Video element `<video>` embeds a media player which supports video playback into the document. You can use `<video>` for audio content as well, but the `<audio>` element may provide a more appropriate user experience.

### [Node.textConten](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent)

`Node.textContent` property represents the text content of a node and its descendants, and can be used to `get` or `set` the text content.

0 comments on commit e1ab708

Please sign in to comment.