Skip to content

Instantly share code, notes, and snippets.

@davidbnk
Created February 14, 2019 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidbnk/be44ae5c01deafaffd1a4f860b3f6f89 to your computer and use it in GitHub Desktop.
Save davidbnk/be44ae5c01deafaffd1a4f860b3f6f89 to your computer and use it in GitHub Desktop.
var args = arguments[0];
if (OS_IOS) {
Ti.Media.setAudioSessionCategory(Ti.Media.AUDIO_SESSION_CATEGORY_PLAYBACK);
}
var currentPlaybackTime = 0;
var currentPlaybackTimeInverval = null;
var playAgainAttempts = 0;
$.videoPlayer.url = args.url;
$.videoPlayer.addEventListener('playbackState', function (e) {
if (e.playbackState === Ti.Media.VIDEO_PLAYBACK_STATE_PLAYING) {
playAgainAttempts = 0;
$.trigger('playing');
if (args.callbackVideoPlaying) {
args.callbackVideoPlaying();
}
}
});
$.videoPlayer.addEventListener('complete', function (e) {
if (OS_ANDROID){
if (e.success) {
$.videoPlayer.hide();
$.videoPlayer.release();
if (args.callbackVideoComplete) {
args.callbackVideoComplete();
}
} else if (playAgainAttempts === 0){
tryPlayAgain();
}
}
});
$.videoPlayer.addEventListener('fullscreen', function (e) {
if (e.entering === 0 || e.entering === false) {
$.videoPlayer.release();
if (args.callbackVideoEnded) {
args.callbackVideoEnded();
}
}
});
if (OS_ANDROID){
$.videoPlayer.addEventListener('error', function (e){
if (playAgainAttempts === 0 && (e.source.playbackState === Ti.Media.VIDEO_PLAYBACK_STATE_INTERRUPTED)){
tryPlayAgain();
}
});
}
$.videoPlayer.stop();
function play() {
if (OS_ANDROID) {
$.trigger('play');
}
if (checkInternetConnection()) {
$.videoPlayer.play();
if (OS_ANDROID){
currentPlaybackTimeInverval = setInterval(function () {
if ($.videoPlayer.currentPlaybackTime !== 0) {
currentPlaybackTime = $.videoPlayer.currentPlaybackTime;
}
}, 500);
}
}
}
function tryPlayAgain(){
playAgainAttempts++;
if (playAgainAttempts < 4 && OS_ANDROID){
$.trigger('tryPlayAgain');
$.videoPlayer.setInitialPlaybackTime(currentPlaybackTime);
if (currentPlaybackTimeInverval !== null) {
clearInterval(currentPlaybackTimeInverval);
}
if (Titanium.Network.online) {
$.videoPlayer.play();
currentPlaybackTimeInverval = setInterval(function () {
if ($.videoPlayer.currentPlaybackTime !== 0) {
currentPlaybackTime = $.videoPlayer.currentPlaybackTime;
}
}, 500);
} else {
setTimeout(tryPlayAgain, 10000);
}
} else {
var alertDialog = Titanium.UI.createAlertDialog({
title: 'No connection',
message: 'No internet connection',
buttonNames: ['OK'],
canceledOnTouchOutside: false
});
alertDialog.addEventListener('click', function (e) {
if (args.callbackVideoEnded) {
args.callbackVideoEnded();
}
$.videoPlayer.hide();
$.videoPlayer.release();
});
alertDialog.show();
}
}
exports.play = play;
function checkInternetConnection() {
return Titanium.Network.online;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment