A small library to get the URL of the desired YouTube video ID to use it natively in Ti.Media.VideoPlayer.
It returns an URL object with the following properties:
{
medium: 'typically 360p',
high: 'typically 720p',
best: 'best quality available either medium or high'
}
When available, ti.youtube will use HLS manifest file to play adaptive media from live streaming.
Run the following command in your lib
directory in Alloy, Resources directory for classic or the project root for Titanium Webpack projects.
npm i ti.youtube
If you don't use npm
, you can download the latest version and place it in your lib
folder (or Resources for classic project).
index.js
const tiYoutube = require('ti.youtube');
tiYoutube.getUrlByVideoId('SMKPKGW083c', url => {
// Available Video Qualities:
// medium ( typically 360p )
// high ( typically 720p )
// best ( best quality available either medium or high )
$.videoPlayer.url = url.best;
}, e => {
// optional callback in case of an error
// can return e.error statuses ["no_valid_urls", "video_not_allowed", "http_error"]
});
$.window.open();
index.xml
<Alloy>
<Window id="window">
<VideoPlayer id="videoPlayer" ns="Ti.Media" />
</Window>
</Alloy>
app.tss
"#window": {
backgroundColor: '#ffffff'
}
"#videoPlayer" : {
autoplay: true,
width: Ti.UI.FILL,
height: Ti.UI.FILL,
showsControls: true
}
"#videoPlayer[platform=ios]" : {
allowsAirPlay: true,
backgroundColor: 'transparent',
mediaTypes: [ Ti.Media.VIDEO_MEDIA_TYPE_VIDEO ]
}
"#videoPlayer[platform=android]" : {
keepScreenOn: true,
backgroundColor: '#000000'
}
Some YouTube videos are restricted by their owners to play only on websites.
The library will show an alert that the given video cannot be played natively.
I will try to export more information from the returned data. Like video title, description, length, thumbnails, etc.
In order to find and resolve media streams, you need to first get video metadata. There are a few ways to do it, but the most reliable one is by querying an AJAX endpoint used internally by YouTube’s iframe embed API. The format is as follows: https://www.youtube.com/get_video_info?video_id={videoId}.
The request can take a lot of different parameters, but at a minimum it needs a video ID — the value in the URL that comes after /watch?v=, for example dQw4w9WgXcQ.
The response contains URL-encoded metadata, which has to be decoded first before it’s usable. After that, you can map the parameter names to values in a dictionary for easier access. Some parameter values are nested objects themselves, so they can in turn be mapped to nested dictionaries.
Source Reverse-Engineering YouTube by Alexey Golub
Apache Version 2.0
See License