Skip to content

Instantly share code, notes, and snippets.

@haroonabbasi
Last active September 15, 2015 06:40
Show Gist options
  • Save haroonabbasi/2b09e2d32ef62048c4d4 to your computer and use it in GitHub Desktop.
Save haroonabbasi/2b09e2d32ef62048c4d4 to your computer and use it in GitHub Desktop.
// media is the video url or the video file blob source
// cb - callback function to receive the thumb image and video duration in seconds
// size - dimension of one side of the required thumb (the other side will be calculated based on aspect ratio)
function getVideoThumb = function(media, cb, size) {
var player = Titanium.Media.createVideoPlayer({
autoplay : false,
media : media,
scalingMode : Ti.Media.VIDEO_SCALING_MODE_FILL
});
setTimeout(function() {
tryToRetrieveVideoThumb(player, cb, size);
}, 1000);
};
function tryToRetrieveVideoThumb(player, cb, size) {
if (player.naturalSize.width == 0) {
setTimeout(function() {
tryToRetrieveVideoThumb(player, cb);
}, 1000);
return;
}
player.width = player.naturalSize.width;
player.height = player.naturalSize.height;
var imageBlob = player.thumbnailImageAtTime(1.0, Ti.Media.VIDEO_TIME_OPTION_EXACT);
if (size) {
var img = _resizePhoto(imageBlob, size),
thumbView = Ti.UI.createView({
width : img.w,
height : img.h,
backgroundImage : img.media
}), playIcon = Ti.UI.createImageView({
image : '/img/video_icon.png',
height : 20,
width : 20
});
thumbView.add(playIcon);
cb(thumbView.toImage(), player.duration / 1000);
} else
cb(imageBlob, player.duration / 1000);
}
function _resizePhoto(media, size) {
var oW = media.width, oH = media.height, w = oW >= oH ? Math.round(Math.min(size, oW)) : Math.round(Math.min(oW * (size / oH), oW)), h = oW <= oH ? Math.round(Math.min(size, oH)) : Math.round(Math.min(oH * (size / oW), oH));
return {
media : media.imageAsResized(w, h),
w : w,
h : h
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment