Skip to content

Instantly share code, notes, and snippets.

@LHIOUI
Last active April 3, 2018 08:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LHIOUI/f9e1503502860064345e to your computer and use it in GitHub Desktop.
Save LHIOUI/f9e1503502860064345e to your computer and use it in GitHub Desktop.
Titanium commonJS module to play youtube video on Ti.Media.VideoPlayer many thanks to The Endless Hack https://github.com/endlesshack/youtube-video
//
// tiYoutube.js
//
// Created by Coyote on 2014-10-07.
// Copyright 2014 Coyote. All rights reserved.
//
var YoutubeVideo = function(id, callback) {
var client = Ti.Network.createHTTPClient();
client.onload = function(video_info) {
Ti.API.debug('on load');
var video;
var json = JSON.stringify(video_info);
var pa = JSON.parse(json);
if (pa.source.responseText.indexOf("video_id") > -1) {
Ti.API.debug('json string ' + pa.source.responseText);
video = decodeQueryString(pa.source.responseText);
if (video.status === "fail") {
return callback(video);
}
video.sources = decodeStreamMap(video.url_encoded_fmt_stream_map);
video.getSource = function(type, quality) {
var exact, key, lowest, source, _ref;
lowest = null;
exact = null;
_ref = this.sources;
for (key in _ref) {
source = _ref[key];
if (source.type.match(type)) {
if (source.quality.match(quality)) {
exact = source;
} else {
lowest = source;
}
}
}
return exact || lowest;
};
return callback(video);
} else
return callback('undefined');
};
client.onerror = function(e) {
Ti.API.error('error' + JSON.stringify(e));
callback('undefined');
};
client.open("GET", "http://www.youtube.com/get_video_info?video_id=" + id);
client.send();
};
var decodeQueryString = function(queryString) {
var key, keyValPair, keyValPairs, r, val, _i, _len;
r = {};
keyValPairs = queryString.split("&");
for ( _i = 0, _len = keyValPairs.length; _i < _len; _i++) {
keyValPair = keyValPairs[_i];
key = decodeURIComponent(keyValPair.split("=")[0]);
val = decodeURIComponent(keyValPair.split("=")[1] || "");
r[key] = val;
}
return r;
};
var decodeStreamMap = function(url_encoded_fmt_stream_map) {
var quality, sources, stream, type, urlEncodedStream, _i, _len, _ref;
sources = {};
_ref = url_encoded_fmt_stream_map.split(",");
for ( _i = 0, _len = _ref.length; _i < _len; _i++) {
urlEncodedStream = _ref[_i];
stream = decodeQueryString(urlEncodedStream);
type = stream.type.split(";")[0];
quality = stream.quality.split(",")[0];
stream.original_url = stream.url;
stream.url = "" + stream.url + "&signature=" + stream.sig;
sources["" + type + " " + quality] = stream;
}
return sources;
};
/**
* @param {Object} id
*/
exports.playYoutube = function(id) {
YoutubeVideo(id, function(video) {
if (video != 'undefined') {
var mp4 = video.getSource("video/mp4", "medium");
Ti.API.debug('mp4 url ' + mp4.url);
var win = Ti.UI.createWindow();
var video = Titanium.Media.createVideoPlayer();
video.url = mp4.url;
win.backgroundColor = "#fff";
win.orientationModes = [Ti.UI.LANDSCAPE_LEFT, Ti.UI.LANDSCAPE_RIGHT];
win.add(video);
win.addEventListener('open', function(e) {
video.play();
});
win.open();
} else
alert('this is not valid Youtoube video');
});
};
//////////////////////////////////////////
// usage :
// var palyYoutube=require('tiYoutube');
// palyYoutube('wGoM_wVrwng');
//////////////////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment