Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-7145] Fixed some bugs in VideoPlayer. Added fullscreen support for Firefox #1788

Merged
merged 1 commit into from
Mar 22, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion apidoc/Titanium/Media/VideoPlayer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,11 @@ properties:
summary: URL of the media to play.
description: |
URL identifying a local or remote video to play.
type: String

On Mobile Web, video format support depends on the web browser. You can specify an
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth mentioning that there isn't a common format across all browsers, and that specifying an array is necessary in practice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's sort of implied by saying the supported video formats depend on the browser. FWIW, Arthur helped me write that sentence, so he has effectively signed off on it. Unless you feel strongly that it needs clarification, Arthur and I are cool with it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong feelings either way...sounds good to me.

array of URLs to videos of different formats. The web browser will select the first
video URL in the array that it is able to play. This is not supported on iOS and Android.
type: [String,Array]

- name: useApplicationAudioSession
summary: |
Expand Down
79 changes: 45 additions & 34 deletions mobileweb/titanium/Ti/Media/VideoPlayer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
define(["Ti/_/declare", "Ti/Media", "Ti/UI/View"],
function(declare, Media, View) {
define(["Ti/_/declare", "Ti/_/dom", "Ti/_/event", "Ti/_/lang", "Ti/Media", "Ti/UI/View"],
function(declare, dom, event, lang, Media, View) {

var on = require.on,
var doc = document,
on = require.on,
prefixes = require.config.vendorPrefixes.dom,
STOPPED = 0,
STOPPING = 1,
PAUSED = 2,
PLAYING = 3,
nativeFullscreen,
requestFullScreen = "requestFullScreen",
exitFullScreen = "exitFullScreen",
nativeFullScreen = (function() {
for (var i = 0, prefix; i < prefixes.length; i++) {
prefix = prefixes[i].toLowerCase();
if (doc[prefix + "CancelFullScreen"]) {
requestFullScreen = prefix + "RequestFullScreen";
exitFullScreen = prefix + "ExitFullScreen";
return 1;
}
}
return !!doc.cancelFullScreen;
}()),
fakeFullscreen = true,
mimeTypes = {
"m4v": "video/mp4",
Expand All @@ -17,6 +31,10 @@ define(["Ti/_/declare", "Ti/Media", "Ti/UI/View"],
"webm": "video/webm"
};

function isFullScreen(fs) {
return nativeFullScreen ? (!!doc.mozFullScreen || !!doc.webkitIsFullScreen) : !!fs;
}

return declare("Ti.Media.VideoPlayer", View, {

_currentState: STOPPED,
Expand All @@ -37,23 +55,17 @@ define(["Ti/_/declare", "Ti/Media", "Ti/UI/View"],
}
},
fullscreen: {
// TODO: Add check for Firefox <http://www.thecssninja.com/javascript/fullscreen>
value: (function(s) {
return s && s();
}(document.createElement("video").webkitDisplayingFullscreen)),
value: isFullScreen(),

set: function(value) {
var h,
v = this._video;

value = !!value;
if (nativeFullscreen) {
if (nativeFullScreen) {
try {
if (value) {
v.webkitEnterFullscreen();
} else {
v.webkitExitFullscreen();
}
value === isFullScreen() && (value = !value);
v[value ? requestFullScreen : exitFullScreen]();
} catch(ex) {}
} else if (fakeFullscreen) {
v.className = value ? "fullscreen" : "";
Expand Down Expand Up @@ -126,21 +138,15 @@ define(["Ti/_/declare", "Ti/Media", "Ti/UI/View"],
this.fireEvent("complete", {
reason: ended ? Media.VIDEO_FINISH_REASON_PLAYBACK_ENDED : Media.VIDEO_FINISH_REASON_USER_EXITED
});
ended && this.repeatMode === Media.VIDEO_REPEAT_MODE_ONE && setTimeout(function() { this._video.play(); }, 1);
ended && this.repeatMode === Media.VIDEO_REPEAT_MODE_ONE && setTimeout(lang.hitch(this, function() { this._video.play(); }), 1);
},

_stalled: function() {
this._set("loadState", Media.VIDEO_LOAD_STATE_STALLED);
},

_fullscreenChange: function(e) {
this.fullscreen && (this.fullscreen = !_fullscreen);
},

_metaDataLoaded: function() {
// TODO: Add check for Firefox <http://www.thecssninja.com/javascript/fullscreen>
nativeFullscreen = this._video.webkitSupportsFullscreen;
this._durationChange();
this.properties.__values__.fullscreen = !isFullScreen(this.fullscreen);
},

_durationChange: function() {
Expand All @@ -167,7 +173,7 @@ define(["Ti/_/declare", "Ti/Media", "Ti/UI/View"],
},

_createVideo: function(dontCreate) {
var i, src, match,
var i, match,
video = this._video,
url = this.url;

Expand All @@ -181,8 +187,9 @@ define(["Ti/_/declare", "Ti/Media", "Ti/UI/View"],

this.release();

video = this._video = document.createElement("video");
video.tabindex = 0;
video = this._video = dom.create("video", {
tabindex: 0
});

this.mediaControlStyle === Media.VIDEO_CONTROL_DEFAULT && (video.controls = 1);
this.scalingMode = Media.VIDEO_SCALING_ASPECT_FIT;
Expand All @@ -208,7 +215,7 @@ define(["Ti/_/declare", "Ti/Media", "Ti/UI/View"],
on(video, "loadeddata", this, function() {
this.fireEvent("load");
}),
on(video, "loadedmetadata", this, "_metaDataLoaded"),
on(video, "loadedmetadata", this, "_durationChange"),
on(video, "durationchange", this, "_durationChange"),
on(video, "timeupdate", this, function() {
this.constants.currentPlaybackTime = this._video.currentTime * 1000;
Expand Down Expand Up @@ -244,11 +251,11 @@ define(["Ti/_/declare", "Ti/Media", "Ti/UI/View"],
require.is(url, "Array") || (url = [url]);

for (i = 0; i < url.length; i++) {
src = document.createElement("source");
src.src = url[i];
match = url[i].match(/.+\.([^\/\.]+?)$/);
match && mimeTypes[match[1]] && (src.type = mimeTypes[match[1]]);
video.appendChild(src);
dom.create("source", {
src: url[i],
type: match && mimeTypes[match[1]]
}, video);
}

return video;
Expand All @@ -271,18 +278,22 @@ define(["Ti/_/declare", "Ti/Media", "Ti/UI/View"],
var i,
video = this._video,
parent = video && video.parentNode;
this._currentState = STOPPED;
this.constants.playing = false;
if (parent) {
require.each(this._handles, function(h) { h(); });
this._handles = [];
event.off(this._handles);
parent.removeChild(video);
}
this._video = null;
},

stop: function() {
var v = this._video;
this._currentState = STOPPING;
this._video.pause();
this._video.currentTime = 0;
if (v) {
v.pause();
v.currentTime = 0;
}
}

});
Expand Down
4 changes: 2 additions & 2 deletions mobileweb/titanium/Ti/UI/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ define(["Ti/_/declare", "Ti/_/UI/TextBox", "Ti/_/css", "Ti/_/dom", "Ti/_/lang",

maxLength: {
set: function(value) {
value = value|0;
this._field.maxlength = value > 0 ? value : "";
value = Math.min(value|0, 0);
dom.attr[value > 0 ? "set" : "remove"](this._field, "maxlength", value);
return value;
}
},
Expand Down