Skip to content

Commit

Permalink
Add playlist playing support
Browse files Browse the repository at this point in the history
  • Loading branch information
julianna-langston committed Dec 15, 2024
1 parent 37bdd25 commit 586d012
Showing 1 changed file with 50 additions and 19 deletions.
69 changes: 50 additions & 19 deletions public/youtube_embed.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ <h2>Troubleshooting</h2>
<script>
let track = [];
let playRate = 1;
const id = new URLSearchParams(location.search).get("id");

(async () => {
let currentlyPlayingId = "";

const loadVideoTranscript = async (id) => {
console.log("Getting transcript for video id", id);
try {
const result = await fetch(`https://raw.githubusercontent.com/julianna-langston/ScreenReaderDescription/main/transcripts/${id}.json`)
const json = await result.json();
Expand All @@ -79,24 +80,48 @@ <h2>Troubleshooting</h2>
}catch(err){
alert("Warning: Requested video does not have audio transcripts available.");
}
})();

}
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player("player", {
height: "390",
width: "640",
videoId: id,
playerVars: {
fs: 0,
playsinline: 1
},
events: {
onReady: onPlayerReady,
onStateChange: onPlayerStateChange,
onPlaybackRateChange: onPlayerPlaybackRateChange,
}
});
const params = new URLSearchParams(location.search);
if(params.has("id")){
const videoId = new URLSearchParams(location.search).get("id");
player = new YT.Player("player", {
height: "390",
width: "640",
videoId,
playerVars: {
fs: 0,
playsinline: 1
},
events: {
onReady: onPlayerReady,
onStateChange: onPlayerStateChange,
onPlaybackRateChange: onPlayerPlaybackRateChange,
}
});
}else{
const playlist = params.get("list");

player = new YT.Player("player", {
height: "390",
width: "640",
playerVars: {
fs: 0,
playsinline: 1,
autoplay: 0,
controls: 1,
listType: "list",
playlist
},
events: {
onReady: onPlayerReady,
onStateChange: onPlayerStateChange,
onPlaybackRateChange: onPlayerPlaybackRateChange,
}
});
}
}

function onPlayerPlaybackRateChange(event){
Expand Down Expand Up @@ -143,6 +168,12 @@ <h2>Troubleshooting</h2>
};

function onPlayerStateChange({ target, data }) {
const playingVideoId = target.playerInfo.videoData.video_id;
if(currentlyPlayingId !== playingVideoId && playingVideoId){
loadVideoTranscript(playingVideoId);
currentlyPlayingId = playingVideoId;
}

if (data === 1) {
playTranscriptFrom(Math.floor(target.getCurrentTime()), playRate);
} else {
Expand Down

0 comments on commit 586d012

Please sign in to comment.