Week 5: Audio/Video API
To Do This Week:
Get GitHub Account & Download GitHub Desktop
Watch this video for assistance: Git, GitHub, & GitHub Desktop for beginners
- Sign-up or Sign-in to a GitHub account.
- Install & Sign In Download GitHub Desktop and install it. Sign in with your GitHub account.
- Create a Repository Go to File → New Repository. Choose a name like "DTC477" and a local folder that just holds your class projects. Click Create Repository.
- Add Project Files You will be adding all class project folders and files to this local folder (you will also upload projects to your server!). GitHub Desktop will detect automatically all new folders and files in this local folder you set up. For now, add a test file to this local folder.
- Commit Changes After adding a file or folder, review the changes in GitHub Desktop. Write a short summary message (e.g., “Initial commit”). Click Commit to main.
- Publish to GitHub Click Publish Repository. Choose Public or Private. Click Publish. Your project is now on GitHub.com.
- Make & Push Updates Edit files as needed. Return to GitHub Desktop, write a commit message, commit, then click Push origin to update GitHub.
- Check on GitHub.com Go to your GitHub repository online. You’ll see your files and commit history.
GitHub Co-pilot is now a free - you need a GitHub repository
Review
Loops: for loop, while loop, do/while loop Iterating through array: forEach() / map()let fruits = ["apple", "strawberry", "banana"];
//forEach() with arrow function
fruits.forEach((fruit, index, array) => {
if (fruit.endsWith("y")) {
array[index] = fruit.slice(0, -1) + "ies";
} else {
array[index] = fruit + "s";
}
});
Reminders
- How to think inside the browser...
- External scripts:
<script src="myScript.js"></script>
<
- Style property: Manipulating CSS with JS
- CSS animation and JS: add/remove classes
- setTimeout, setInterval: Timed execution
- window.scrollTo(): Scrolling control
- Graphic elements: progress bar, range slider, flip card
- reload page: location.href = "currentpage.html";
Random Numbers:
Generate a random number in a range:// Random generator
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
getRandomInt(5, 10);
Audio/Video Element & JavaScript:
Important notes:- Chrome requires user interaction (e.g., a click) before video or audio can play.
- Learn more about Video and Audio elements.
- Audio/Video DOM reference
- fullscreen video, responsive videos
Code samples
Jump to a specific time.
videoElement.currentTime = 10; // jump to 10 seconds
Sync audio to the current video time (works for multiple media elements too).
audioElement.currentTime = videoElement.currentTime;
Slow down or speed up playback.
videoElement.playbackRate = 2.0; // double speed
videoElement.playbackRate = 0.5; // half speed
Trigger code when the video passes certain timestamps.
videoElement.addEventListener("timeupdate", function () {
if (videoElement.currentTime > 5 && videoElement.currentTime < 6) {
console.log("Triggered at ~5 seconds");
}
});
Loop only a specific section (from 5s to 10s).
videoElement.addEventListener("timeupdate", function () {
if (videoElement.currentTime > 10) {
videoElement.currentTime = 5; // loop back
}
});
Play and pause the video.
videoElement.play();
videoElement.pause();
Restart from the beginning and play.
videoElement.currentTime = 0;
videoElement.play();
Toggle play/pause with one button.
if (videoElement.paused) {
videoElement.play();
} else {
videoElement.pause();
}
Mute and unmute.
videoElement.muted = true; // mute
videoElement.muted = false; // unmute
Play the video in a loop.
videoElement.loop = true;
Store timestamps and labels as objects (e.g., for lyrics or captions).
const lyrics = [
{ time: 0, text: "First line of the song" },
{ time: 5, text: "Second line of the song" }
];
Audio/Video Challenge
- Select One of the Challenges below. You are to use these in a web page with Javascript. Pick one of the challenges and create a solution by thinking through step by step what you want the script to do. Use JavaScript language in your steps.The focus is on Javascript not on design with CSS.
- Download this ZIP folder for sample video/audio files.
- Main Concepts Audio/Video elements, DOM manipulation, event listeners, and time-based updates.
1. Video & Audio Tag Filter (Sort & Display by Category)
Challenge: Create a system where users can filter and display audio or video clips based on categories or tags (e.g., "spooky," "happy" "sad"). Users select a tag, and only matching media clips appear on the page.
When the user selects a category, the script dynamically updates the display, filtering the video or audio elements accordingly.
Could be used for media libraries, educational video sorting, or interactive storytelling experiences.
2. Custom Video Player with Interactive Features
Challenge: Build a custom video player with additional features beyond the basic play/pause buttons. Allow users to control playback, change speed, apply effects, or modify audio/video settings dynamically.
The player could include:
- Speed controls (slow motion, fast-forward).
- Visual effects (CSS filters, color shifts, glitch effects).
- Custom buttons for unique interactions (e.g., "Invert Colors," "Echo Audio").
Could be used to create a uniquely styled video experience, an interactive film, or a themed media player.
3. Time & Scroll-Based Interactive Triggers
Challenge: Create an interactive experience where videos and/or sounds trigger events based on time in a video or scrolling on a webpage.
The script detects either the user's scroll position or specific timestamps in a video/audio file, then modifies HTML elements, plays sounds, or adds animations dynamically.
Could be used for interactive lectures, storytelling, or music-driven visual effects.
4. Video Audio Remix (Swap, Mix, or Manipulate Sounds)
Challenge: Allow users to swap background audio tracks for different moods while watching the same video. The script should sync the video with different audio files, letting users change the vibe on the fly.
Users can pick between multiple audio tracks (e.g., "Lo-fi Chill," "Synthwave," "Epic Orchestral") and swap them in real time.
Could be used for remixing experiences, interactive film scoring, or dynamic sound experimentation.
5. Video Memory Challenge (What Did You See?)
Challenge: Create a memory game where users watch a short video clip that then disappears. Afterward, they must recall and name objects or details from the video.
The script plays the video for a set time, then hides it and prompts the user with multiple-choice questions or an input field to test their memory.
Could be used for observation training, quick-recall challenges, or educational memory tests.
Objects and Object Literals
Objects are variables that store properties and methods.
JSON (JavaScript Object Notation) is a format for storing and exchanging data. It looks similar to a JavaScript object and behaves in a comparable way.
Simple Object Literal
// JavaScript Object Literal Example
const person = {
firstName: "Margie",
lastName: "Pepper",
age: 50,
eyeColor: "brown",
};
console.log(`Hi, ${person.firstName}`);
Object Literal with "this"
// JavaScript Object Literal Example
const person = {
firstName: "Henry",
lastName: "Smith",
age: 24,
eyeColor: "green",
fullName() {
return `${this.firstName} ${this.lastName}`;
}
};
console.log(`Hi, ${person.fullName()}`);
Person Object Constructor
// JavaScript Object Constructor Example
function Person(firstName, lastName, age, eyeColor) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.eyeColor = eyeColor;
this.fullName = function() {
return `${this.firstName} ${this.lastName}`;
};
}
// Create new Person objects
const person1 = new Person("Alice", "Brown", 32, "blue");
const person2 = new Person("Carlos", "Diaz", 28, "hazel");
console.log(person1.fullName()); // "Alice Brown"
console.log(person2.fullName()); // "Carlos Diaz"
Learn more about JSON and objects:
GitHub Introduction
GitHub Intro- GitHub - a social and collaborative platform for development
- Git - version control system for tracking changes in computer files and coordinating work on those files among multiple people. (command-line install)
- desktop.github no command line needed
- Github guide
- Install & Sign In Download GitHub Desktop and install it. Sign in with your GitHub account.
- Create a Repository Go to File → New Repository. Choose a name and local folder. Optionally add a README and .gitignore. Click Create Repository.
- Add or Create Project Files Place existing project files into the local folder, or create new files there. GitHub Desktop will detect them automatically.
- Commit Changes Review the changes in GitHub Desktop. Write a short summary message (e.g., “Initial commit”). Click Commit to main.
- Publish to GitHub Click Publish Repository. Choose Public or Private. Click Publish. Your project is now on GitHub.com.
- Make & Push Updates Edit files as needed. Return to GitHub Desktop, write a commit message, commit, then click Push origin to update GitHub.
- Check on GitHub.com Go to your GitHub repository online. You’ll see your files and commit history.
- Repository: A repository is the most basic element of GitHub. They're easiest to imagine as a project's folder. A repository contains all of the project files (including documentation), and stores each file's revision history. Repositories can have multiple collaborators and can be either public or private.
- Branch:A branch is a parallel version of a repository. It is contained within the repository, but does not affect the primary or main branch allowing you to work freely without disrupting the "live" version. When you've made the changes you want to make, you can merge your branch back into the main branch to publish your changes.
- Merge: Merging takes the changes from one branch (in the same repository or from a fork), and applies them into another. This often happens as a "pull request" (which can be thought of as a request to merge), or via the command line. A merge can be done through a pull request via the GitHub.com web interface if there are no conflicting changes, or can always be done via the command line.
- Clone: A clone is a copy of a repository that lives on your computer instead of on a website's server somewhere, or the act of making that copy. When you make a clone, you can edit the files in your preferred editor and use Git to keep track of your changes without having to be online. The repository you cloned is still connected to the remote version so that you can push your local changes to the remote to keep them synced when you're online.
- Pull: Pull refers to when you are fetching in changes and merging them. For instance, if someone has edited the remote file you're both working on, you'll want to pull in those changes to your local copy so that it's up to date. See also fetch.
- Pull request: Pull requests are proposed changes to a repository submitted by a user and accepted or rejected by a repository's collaborators. Like issues, pull requests each have their own discussion forum.
- Fork: A fork is a personal copy of another user's repository that lives on your account. Forks allow you to freely make changes to a project without affecting the original upstream repository. You can also open a pull request in the upstream repository and keep your fork synced with the latest changes since both repositories are still connected.
- Fetch: When you use
git fetch, you're adding changes from the remote repository to your local working branch without committing them. Unlikegit pull, fetching allows you to review changes before committing them to your local branch. - Push: To push means to send your committed changes to a remote repository on GitHub.com. For instance, if you change something locally, you can push those changes so that others may access them.
- Commit: A commit, or "revision", is an individual change to a file (or set of files). When you make a commit to save your work, Git creates a unique ID (a.k.a. the "SHA" or "hash") that allows you to keep record of the specific changes committed along with who made them and when. Commits usually contain a commit message which is a brief description of what changes were made.
- Markdown: Markdown is an incredibly simple semantic file format, not too dissimilar from .doc, .rtf and .txt. Markdown makes it easy for even those without a web-publishing background to write prose (including with links, lists, bullets, etc.) and have it displayed like a website. GitHub supports Markdown and uses a particular form of Markdown called GitHub Flavored Markdown. See GitHub Flavored Markdown Spec or Getting started with writing and formatting on GitHub.markup