Last active
August 29, 2015 14:09
-
-
Save sebgeelen/9bffe43105456d4cb82d to your computer and use it in GitHub Desktop.
Load a video file content and base64 encode it into a videoElement src
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//--- | |
//------------------------------------------------------------------------------------------------- | |
// Create the very first VideoElement in the dom | |
// It would later be used to know the width and | |
// height of the cloned videoData objects | |
Future<VideoData> load(String url, { | |
bool mp4: true, bool ogg: true, bool webm: true}) { | |
VideoElement video; | |
var onCanPlaySubscription; | |
var onErrorSubscription; | |
var videoUrls = _getOptimalVideoUrls(url, mp4, ogg, webm); | |
var loadCompleter = new Completer<VideoData>(); | |
print("video path : ${videoUrls}"); | |
onCanPlay(event) { | |
onCanPlaySubscription.cancel(); | |
onErrorSubscription.cancel(); | |
video.width = video.videoWidth; | |
video.height = video.videoHeight; | |
var videoData = new VideoData(video); | |
loadCompleter.complete(videoData); | |
}; | |
onData(HttpRequest request) { | |
FileReader reader = new FileReader(); | |
reader.readAsDataUrl(request.response); | |
reader.onLoadEnd.first.then((e){ | |
if(reader.readyState != FileReader.DONE) { | |
throw 'Error while reading ${url}'; | |
} | |
video = new VideoElement(); | |
onCanPlaySubscription = video.onCanPlayThrough.listen(onCanPlay); | |
onErrorSubscription = video.onError.listen((_){ | |
loadCompleter.completeError(new StateError("Failed to create video with data.")); | |
}); | |
video.src = reader.result; | |
video.load(); | |
}); | |
}; | |
onError(event) { | |
print("+ error grabing try next : ${event}"); | |
if (videoUrls.length > 0) { | |
print("+ grab ${videoUrls[0]}"); | |
HttpRequest.request(videoUrls.removeAt(0), responseType: 'blob') | |
.then(onData) | |
.catchError(onError); | |
} else { | |
loadCompleter.completeError(new StateError("Failed to load uri.")); | |
} | |
}; | |
print("+ grab ${videoUrls[0]}"); | |
HttpRequest.request(videoUrls.removeAt(0), responseType: 'blob') | |
.then(onData) | |
.catchError(onError); | |
return loadCompleter.future; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment