Skip to content

Commit f0ac93e

Browse files
committed
Device.oggVideo indicates if the browser can play back ogg video files.
Device.h264Video indicates if the browser can play back H264 (mp4) video files. Device.mp4Video indicates if the browser can play back H264 (mp4) video files. Device.webmVideo indicates if the browser can play back webm video files with the vp8 codec. Device.vp9Video indicates if the browser can play back webm video files with the vp9 codec. Device.hlsVideo indicates if the browser can play back mpeg video files.
1 parent 420272d commit f0ac93e

1 file changed

Lines changed: 117 additions & 6 deletions

File tree

src/system/Device.js

Lines changed: 117 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,44 @@ Phaser.Device = function () {
373373
*/
374374
this.webm = false;
375375

376+
// Video
377+
378+
/**
379+
* @property {boolean} oggVideo - Can this device play ogg video files?
380+
* @default
381+
*/
382+
this.oggVideo = false;
383+
384+
/**
385+
* @property {boolean} h264Video - Can this device play h264 mp4 video files?
386+
* @default
387+
*/
388+
this.h264Video = false;
389+
390+
/**
391+
* @property {boolean} mp4Video - Can this device play h264 mp4 video files?
392+
* @default
393+
*/
394+
this.mp4Video = false;
395+
396+
/**
397+
* @property {boolean} webmVideo - Can this device play webm video files?
398+
* @default
399+
*/
400+
this.webmVideo = false;
401+
402+
/**
403+
* @property {boolean} vp9Video - Can this device play vp9 video files?
404+
* @default
405+
*/
406+
this.vp9Video = false;
407+
408+
/**
409+
* @property {boolean} hlsVideo - Can this device play hls video files?
410+
* @default
411+
*/
412+
this.hlsVideo = false;
413+
376414
// Device
377415

378416
/**
@@ -890,6 +928,47 @@ Phaser.Device._initialize = function () {
890928

891929
}
892930

931+
/**
932+
* Check video support.
933+
*/
934+
function _checkVideo () {
935+
936+
var videoElement = document.createElement("video");
937+
var result = false;
938+
939+
try {
940+
if (result = !!videoElement.canPlayType)
941+
{
942+
if (videoElement.canPlayType('video/ogg; codecs="theora"').replace(/^no$/, ''))
943+
{
944+
device.oggVideo = true;
945+
}
946+
947+
if (videoElement.canPlayType('video/mp4; codecs="avc1.42E01E"').replace(/^no$/, ''))
948+
{
949+
// Without QuickTime, this value will be `undefined`. github.com/Modernizr/Modernizr/issues/546
950+
device.h264Video = true;
951+
device.mp4Video = true;
952+
}
953+
954+
if (videoElement.canPlayType('video/webm; codecs="vp8, vorbis"').replace(/^no$/, ''))
955+
{
956+
device.webmVideo = true;
957+
}
958+
959+
if (videoElement.canPlayType('video/webm; codecs="vp9"').replace(/^no$/, ''))
960+
{
961+
device.vp9Video = true;
962+
}
963+
964+
if (videoElement.canPlayType('application/x-mpegURL; codecs="avc1.42E01E"').replace(/^no$/, ''))
965+
{
966+
device.hlsVideo = true;
967+
}
968+
}
969+
} catch (e) {}
970+
}
971+
893972
/**
894973
* Check audio support.
895974
*/
@@ -1065,6 +1144,7 @@ Phaser.Device._initialize = function () {
10651144
// Run the checks
10661145
_checkOS();
10671146
_checkAudio();
1147+
_checkVideo();
10681148
_checkBrowser();
10691149
_checkCSS3D();
10701150
_checkDevice();
@@ -1084,27 +1164,58 @@ Phaser.Device._initialize = function () {
10841164
*/
10851165
Phaser.Device.canPlayAudio = function (type) {
10861166

1087-
if (type == 'mp3' && this.mp3)
1167+
if (type === 'mp3' && this.mp3)
1168+
{
1169+
return true;
1170+
}
1171+
else if (type === 'ogg' && (this.ogg || this.opus))
1172+
{
1173+
return true;
1174+
}
1175+
else if (type === 'm4a' && this.m4a)
10881176
{
10891177
return true;
10901178
}
1091-
else if (type == 'ogg' && (this.ogg || this.opus))
1179+
else if (type === 'opus' && this.opus)
10921180
{
10931181
return true;
10941182
}
1095-
else if (type == 'm4a' && this.m4a)
1183+
else if (type === 'wav' && this.wav)
1184+
{
1185+
return true;
1186+
}
1187+
else if (type === 'webm' && this.webm)
1188+
{
1189+
return true;
1190+
}
1191+
1192+
return false;
1193+
1194+
};
1195+
1196+
/**
1197+
* Check whether the host environment can play video files.
1198+
*
1199+
* @method canPlayVideo
1200+
* @memberof Phaser.Device.prototype
1201+
* @param {string} type - One of 'mp4, 'ogg', 'webm' or 'mpeg'.
1202+
* @return {boolean} True if the given file type is supported by the browser, otherwise false.
1203+
*/
1204+
Phaser.Device.canPlayVideo = function (type) {
1205+
1206+
if (type === 'webm' && (this.webmVideo || this.vp9Video))
10961207
{
10971208
return true;
10981209
}
1099-
else if (type == 'opus' && this.opus)
1210+
else if (type === 'mp4' && this.mp4Video)
11001211
{
11011212
return true;
11021213
}
1103-
else if (type == 'wav' && this.wav)
1214+
else if (type === 'ogg' && this.oggVideo)
11041215
{
11051216
return true;
11061217
}
1107-
else if (type == 'webm' && this.webm)
1218+
else if (type === 'mpeg' && this.hlsVideo)
11081219
{
11091220
return true;
11101221
}

0 commit comments

Comments
 (0)