Skip to content

Instantly share code, notes, and snippets.

@19h
Last active November 11, 2015 16:24
Show Gist options
  • Save 19h/3151702 to your computer and use it in GitHub Desktop.
Save 19h/3151702 to your computer and use it in GitHub Desktop.
Titanium Mobile SDK iPhone / Android: Extract Youtube Video URL / URI from video-id / identification / name and play mp4 file. (Workaround for the recent change of security in Youtube)
Titanium.App.addEventListener("playvideo", function (e) {
var win11 = Titanium.UI.createWindow({
orientationModes: [Ti.UI.LANDSCAPE_LEFT, Ti.UI.LANDSCAPE_RIGHT],
title: "Video",
zIndex: 222222
});
var activeMovie = Titanium.Media.createVideoPlayer({
fullscreen: !0,
autoplay: !0,
backgroundColor: '#111',
mediaControlStyle: Titanium.Media.VIDEO_CONTROL_DEFAULT,
scalingMode: Titanium.Media.VIDEO_SCALING_ASPECT_FIT
});
activeMovie.url = e.url;
win11.add(activeMovie);
win11.open({
fullscreen: !0
});
activeMovie.play();
});
var vdrld = function (a, b) {
vdldr = Ti.Network.createHTTPClient();
vdldr.onload = function () {
var x = decodeURIComponent(decodeURIComponent(decodeURIComponent(decodeURIComponent(this.responseText.substring(4, this.responseText.length))))),
y = JSON.parse(x).content.video["fmt_stream_map"][0].url;
return b(y);
};
vdldr.setRequestHeader("Referer", "http://www.youtube.com/watch?v=" + a);
vdldr.setRequestHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.14 (KHTML, like Gecko) Version/6.0.1 Safari/536.26.14");
vdldr.open("GET", "http://m.youtube.com/watch?ajax=1&feature=related&layout=mobile&tsp=1&&v=" + a);
vdldr.send()
};
setTimeout(function(){
vdrld("FGAyjfwK-wI", function () {
return Titanium.App.fireEvent("playvideo", {url: arguments[0]});
});
}, 5000);
@demobo-com
Copy link

Does it work in Android also? Seems not

@ademahendra
Copy link

I've got a return failed parse JSON

and I've fix with change

x = decodeURIComponent(decodeURIComponent(decodeURIComponent(decodeURIComponent(this.responseText.substring(4, this.responseText.length)))));
y = JSON.parse(x).content.video["fmt_stream_map"][0].url;

to:

x = this.responseText.substring(4, this.responseText.length);
y = JSON.parse(x).content.player_data["fmt_stream_map"][0].url;

I just remove "decodeURIComponent"

It's work on good on iOS simulator and device. not yet test on android.

@GustavoMSevero
Copy link

ademahendra?

I had the same problem that you and I did the same change you did and I get an error:
"Unable to parse JSON atring at line 60".
This is my line 60:
"y = JSON.parse(x).content.player_data["fmt_stream_map"][0].url;"

What do you suggest that I can do?

Thank you.

@GustavoMSevero
Copy link

I already tried this codes:

var vidWin = Titanium.UI.createWindow({
title : 'Video View Demo',
backgroundColor : '#fff'
});

// check for network
if (Titanium.Network.networkType === Titanium.Network.NETWORK_NONE) {
Titanium.API.info(' no connection ');
} else {
Titanium.API.info(' connection present ');
}

var videoPlayer = Titanium.Media.createVideoPlayer({
top : 50,
autoplay : true,
backgroundColor : 'blue',
height : 300,
width : 300,
mediaControlStyle : Titanium.Media.VIDEO_CONTROL_DEFAULT,
scalingMode : Titanium.Media.VIDEO_SCALING_ASPECT_FIT
});

videoPlayer.url = 'http://www.youtube.com/watch?v=7yTGXoHsHws'; // replace this url with your video url
vidWin.add(videoPlayer);
vidWin.open();

And almost make the video run... The problem is that appears a loading icon running but the video doesn't play!
To play videos from youtube all video has to load before, using Titanium? I don't think so, right?

@macCesar
Copy link

Kenan... Any tips on how to fix your script to work with YouTube's V3 API??

Thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment