Skip to content

Commit

Permalink
Merge pull request zen-audio-player#206 from hjh17/master
Browse files Browse the repository at this point in the history
Closes zen-audio-player#193 Add support for linking to a specific time position
  • Loading branch information
shakeelmohamed authored Jul 4, 2016
2 parents 94e62ea + 2ba7031 commit d729019
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions js/everything.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ var ZenPlayer = {
that.videoDescription = that.getVideoDescription(videoID);
that.videoUrl = plyrPlayer.plyr.embed.getVideoUrl();

// Updates the time position by a given argument in URL
// IE https://zenplayer.audio/?v=koJv-j1usoI&t=30 starts at 0:30
var t = getCurrentTimePosition();
if (t) {
that.videoPosition = t;
window.sessionStorage[videoID] = t;
}

// Initialize UI
that.setupTitle();
that.setupVideoDescription();
Expand Down Expand Up @@ -206,10 +214,10 @@ var ZenPlayer = {

plyrPlayer.addEventListener("timeupdate", function() {
// Store the current time of the video.
var resumeTime = 0;
if (window.sessionStorage) {
var currentTime = plyrPlayer.plyr.embed.getCurrentTime();
var videoDuration = plyrPlayer.plyr.embed.getDuration();
var resumeTime = 0;

// Only store the current time if the video isn't done
// playing yet. If the video finished already, then it
Expand All @@ -221,6 +229,15 @@ var ZenPlayer = {
}
window.sessionStorage[videoID] = resumeTime;
}
var updatedUrl = that.videoUrl;
if (resumeTime > 0) {
updatedUrl = that.videoUrl + "&t=" + Math.round(resumeTime);
$("#zen-video-title").attr("href", updatedUrl);
}
else if (resumeTime <= 0 && $("#zen-video-title").attr("href") !== that.videoUrl) {
updatedUrl = that.videoUrl;
}
$("#zen-video-title").attr("href", updatedUrl);
});

plyrPlayer.plyr.source({
Expand Down Expand Up @@ -360,6 +377,14 @@ function getCurrentVideoID() {
return v;
}

function getCurrentTimePosition() {
var t = parseInt(getParameterByName(window.location.search, "t"), 10);
if (t > 0 && t < Number.MAX_VALUE) {
return t;
}
return 0;
}

function getCurrentSearchQuery() {
var q = getParameterByName(window.location.search, "q");
return q;
Expand All @@ -372,12 +397,15 @@ function removeSearchQueryFromURL(url) {
return url;
}

function makeListenURL(videoID) {
function makeListenURL(videoID, videoPosition) {
var url = removeSearchQueryFromURL(window.location.href);
// Remove any #s which break functionality
url = url.replace("#", "");

return url + "?v=" + videoID;
url += "?v=" + videoID;
if (videoPosition) {
url += "&t=" + videoPosition;
}
return url;
}

function makeSearchURL(searchQuery) {
Expand Down Expand Up @@ -501,13 +529,16 @@ $(function() {
// Handle form submission
$("#form").submit(function(event) {
event.preventDefault();

var formValue = $.trim($("#v").val());
var formValueTime = /&t=(\d*)$/g.exec(formValue);
if (formValueTime && formValueTime.length > 1) {
formValueTime = parseInt(formValueTime[1], 10);
formValue = formValue.replace(/&t=\d*$/g, "");
}
if (formValue) {
var videoID = wrapParseYouTubeVideoID(formValue, true);
ga("send", "event", "form submitted", videoID);
sendKeenEvent("Form submitted", {videoID: videoID});

if (isFileProtocol()) {
errorMessage.show("Skipping video lookup request as we're running the site locally.");
}
Expand All @@ -527,7 +558,7 @@ $(function() {
window.location.href = makeSearchURL(formValue);
}
else {
window.location.href = makeListenURL(videoID);
window.location.href = makeListenURL(videoID, formValueTime);
}
}
}).fail(function(jqXHR, textStatus, errorThrown) {
Expand Down Expand Up @@ -561,7 +592,6 @@ $(function() {
sendKeenEvent("demo", {action: "already had video ID in URL"});
}
});

// Load the player
ZenPlayer.init(currentVideoID);
});
Expand Down

0 comments on commit d729019

Please sign in to comment.