Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load Video by URL #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class YouTubePlayer extends EventEmitter {
start: 0
}, opts)

this.videoId = null
this.videoKey = null
this.destroyed = false

this._api = null
Expand Down Expand Up @@ -99,16 +99,25 @@ class YouTubePlayer extends EventEmitter {

// If load(videoId, [autoplay, [size]]) was called before Iframe API
// loaded, ensure it gets called again now
if (this.videoId) this.load(this.videoId, this._autoplay, this._start)
if (this.videoKey) this.load(this.videoKey, this._autoplay, this._start)
})
}

load (videoId, autoplay = false, start = 0) {
/**
* @param {string} videokey Can be an Id or Url Video
* @param {boolean} autoplay set true if you want play the video on load
* @param {number} start timecode to start the video
* @returns
*/
load (videoKey, autoplay = false, start = 0) {
const isUrl = this._isUrl(videoKey)

if (this.destroyed) return

this.videoId = videoId
this.videoKey = isUrl ? this._getIdFromUrl(videoKey) : videoKey
this._autoplay = autoplay
this._start = start
const videoId = this.videoKey

// If the Iframe API is not ready yet, do nothing. Once the Iframe API is
// ready, `load(this.videoId)` will be called.
Expand All @@ -119,7 +128,6 @@ class YouTubePlayer extends EventEmitter {
this._createPlayer(videoId)
return
}

// If the player instance is not ready yet, do nothing. Once the player
// instance is ready, `load(this.videoId)` will be called. This ensures that
// the last call to `load()` is the one that takes effect.
Expand Down Expand Up @@ -228,7 +236,7 @@ class YouTubePlayer extends EventEmitter {
this._player.destroy()
}

this.videoId = null
this.videoKey = null

this._id = null
this._opts = null
Expand Down Expand Up @@ -443,7 +451,7 @@ class YouTubePlayer extends EventEmitter {
// 3. `load(videoId, [autoplay])` was called multiple times before the player
// was ready. Therefore, the player was initialized with the wrong videoId,
// so load the latest videoId and potentially autoplay it.
this.load(this.videoId, this._autoplay, this._start)
this.load(this.videoKey, this._autoplay, this._start)

this._flushQueue()
}
Expand Down Expand Up @@ -509,7 +517,7 @@ class YouTubePlayer extends EventEmitter {
code === YOUTUBE_ERROR.UNPLAYABLE_2 ||
code === YOUTUBE_ERROR.NOT_FOUND ||
code === YOUTUBE_ERROR.INVALID_PARAM) {
return this.emit('unplayable', this.videoId)
return this.emit('unplayable', this.videoKey)
}

// Unexpected error, does not match any known type
Expand All @@ -532,6 +540,19 @@ class YouTubePlayer extends EventEmitter {
clearInterval(this._interval)
this._interval = null
}

_isUrl (key) {
if (key && (key.includes('youtube.com') || key.includes('youtu.be'))) {
return true
}
return false
}

_getIdFromUrl (videoUrl) {
const getEntireIdQueryRegex = /(v)([=])([\w\d_-]+)([?&])?/g
const cleanQueryRulesRegex = /(?:v=|&)/g
return videoUrl.match(getEntireIdQueryRegex)[0].replace(cleanQueryRulesRegex, '')
}
}

module.exports = YouTubePlayer