HTML5 API - Video



아래는 지난 포스트에 이어 Video 객체의 모든 기능을 모듈화 시킨 코드이며, 추가된 몇 가지 맴버(poster, width, height, onended)를 제외한 나머지는 Audio 객체의 맴버와 동일합니다.


아래 소스를 이용하여 Video 객체의 모든 기능을 테스트 하실 수 있습니다.


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title>Video</title>
  5.     <style type="text/css">
  6.        
  7.     * html
  8.     {
  9.         margin:0;
  10.         padding:0;
  11.     }
  12.    
  13.    
  14.     body
  15.     {
  16.         width:100%;
  17.         height:100%;
  18.     }
  19.     </style>
  20. </head>
  21. <body>
  22. <script type="text/javascript">
  23. //<![CDATA[
  24.  
  25.     var Video = (function (win, doc) {
  26.  
  27.         return function (opt) {
  28.  
  29.             function init() {
  30.  
  31.                 this.options = {
  32.                     player: null,
  33.  
  34.                     wrap: null,
  35.  
  36.                     id: '',
  37.                     src: '',
  38.                     poster: '',
  39.                     width: 500,
  40.                     height: 344,
  41.                     autoplay: false,
  42.                     controls: false,
  43.                     loop: false,
  44.                     isloaded: false,
  45.                     onload: function(){ ; },
  46.                     onplay: function () { ; },
  47.                     onprogress: function () { ; },
  48.                     ontimeupdate: function() { ; },
  49.                     onended: function () { ; },
  50.                     onerror: function () { ; }
  51.  
  52.                 };
  53.  
  54.                 extend.call(this.options, opt);
  55.  
  56.                 // audio load
  57.                 load.call(this.options);
  58.  
  59.                 return this;
  60.             }
  61.  
  62.             Video.fn = init.prototype = {
  63.                 play: play,
  64.                 stop: stop,
  65.                 isEnded: isEnded,
  66.                 getTimeout: getTimeout,
  67.                 getStartTime: getStartTime,
  68.                 getSrc: getSrc,
  69.                 isBuffering: isBuffering,
  70.                 getCurrentTimeout: getCurrentTimeout,
  71.                 setVolume: setVolume,
  72.                 setMuted: setMuted,
  73.                 getVideoSize: getVideoSize
  74.  
  75.             }
  76.  
  77.             return new init();
  78.         };
  79.  
  80.         // audio 객체를 생성한다.
  81.         function createElement() {
  82.  
  83.             this.player = document.createElement('video');
  84.  
  85.             this.player.id = this.id;
  86.             this.player.src = this.src;
  87.  
  88.             // 미디어 클립이 로드되는 동안 컨텐츠 대신 보여질 이미지 파일의 URL을 지정한다.
  89.             this.player.poster = this.poster;
  90.             // 미디어 클립의 가로폭
  91.             this.player.width = this.width;
  92.             // 미디어 클립의 세로폭
  93.             this.player.height = this.height;
  94.  
  95.             this.player.autoplay = this.autoplay;
  96.             this.player.controls = this.controls;
  97.             this.player.loop = this.loop;
  98.  
  99.             var that = this;
  100.  
  101.             // 미디어클립을 재생할 준비가 완료 시 이벤트 핸들러
  102.             bind(this.player, 'canplay', function () {
  103.  
  104.                 that.onload.call(null);
  105.                 this.isloaded = true;
  106.             });
  107.  
  108.             // 미디어클립 재생 클릭 시 이벤트 핸들러
  109.             bind(this.player, 'play', function () { that.onplay.call(null); });
  110.             // 브라우저가 미디어클립 데이터를 가져오는 프로세스에 있을 시 이벤트 핸들러
  111.             bind(this.player, 'progress', function () { that.onprogress.call(null); });
  112.             // 미디어 클립 재생 시 지속적으로 발생(재생 위치가 변경되었을때)
  113.             bind(this.player, 'timeupdate', function () { that.ontimeupdate.call(null); });
  114.             // 미디어 클립 종료 시 이벤트 핸들러
  115.             bind(this.player, 'ended', function () { that.onended.call(null); });
  116.             // 미디어 클립 로드 오류 시 이벤트 핸들러
  117.             bind(this.player, 'error', function () { that.onerror.call(null); });
  118.  
  119.             return this.player;
  120.         };
  121.  
  122.         // audio 객체를 로드합니다.
  123.         function load() {
  124.            
  125.             var elem = createElement.call(this);
  126.  
  127.             if (elem.load) (this.wrap || document.body).appendChild(elem);
  128.             else alert('Video 객체를 지원하지 않는 브라우저 입니다.');
  129.         };
  130.  
  131.         // 미디어클립을 재생합니다.
  132.         function play() {
  133.  
  134.             this.options.player.play();
  135.  
  136.             return this;
  137.         };
  138.  
  139.         // 미디어클립 재생을 중지합니다.
  140.         function stop() {
  141.  
  142.             !this.options.player.paused && this.options.player.pause();
  143.             return this;
  144.         }
  145.  
  146.         // 미디어클립 재생 여부를 반환
  147.         function isEnded() {
  148.             return this.options.player.ended;
  149.         };
  150.  
  151.  
  152.         // 미디어클립의 총 재생시간을 반환
  153.         function getTimeout(type) {
  154.  
  155.             type = type || 'hours';
  156.  
  157.             return {
  158.                 'second': this.options.player.startTime,
  159.                 'hours': getDateSecondToHours(this.options.player.duration)
  160.             }[type];
  161.         };
  162.  
  163.         // 미디어클립의 현재 재생시간을 반환
  164.         function getCurrentTimeout(type) {
  165.  
  166.             type = type || 'hours';
  167.  
  168.             return {
  169.                 'second': this.options.player.currentTime,
  170.                 'hours': getDateSecondToHours(this.options.player.currentTime)
  171.             }[type];
  172.         };
  173.  
  174.         // 미디어 클립의 현재 재생 시간을 반환
  175.         // 0.0(min) ~ 1(max) 할당한다.
  176.         function setVolume(num) {
  177.  
  178.             num = num || 0;
  179.  
  180.             this.options.player.volume = num / 10;
  181.  
  182.             return this;
  183.         };
  184.  
  185.         // 음소거 여부를 설정한다.
  186.         function setMuted(b) {
  187.  
  188.             b = b || false;
  189.  
  190.             this.options.player.muted = b;
  191.  
  192.             return this;
  193.         };
  194.  
  195.         // 미디어 클립이 재생을 시작할 수 있는 가능한 가장 빠른 시간을 반환합니다.
  196.         // 미디어 클립이 스트리밍되거나, 버퍼링되지 않으면 0을 반환합니다.
  197.         function getStartTime(type) {
  198.  
  199.             return this.options.player.startTime;
  200.         };
  201.  
  202.         // 현재 버퍼링 상태 여부를 반환
  203.         function isBuffering() {
  204.             return this.options.player.startTime !== 0;
  205.         };
  206.  
  207.         // 현재 재생중인 파일명을 반환합니다.
  208.         function getSrc() {
  209.             return this.options.player.currentSrc;
  210.         };
  211.  
  212.         // 미디어클립의 사이즈를 반환한다.
  213.         function getVideoSize() {
  214.             return {
  215.                 w: this.options.player.videoWidth,
  216.                 h: this.options.player.videoHeight
  217.             }
  218.         }
  219.  
  220.  
  221.         function getDateSecondToHours(i) {
  222.  
  223.             i = i || 0;
  224.  
  225.             var h = parseInt(((i / 3600) % 24), 10)
  226.               , m = parseInt(((i / 60) % 60), 10)
  227.               , s = parseInt(((i / 1) % 60), 10);
  228.  
  229.             h = h < 10 ? '0' + h : h;
  230.             m = m < 10 ? '0' + m : m;
  231.             s = s < 10 ? '0' + s : s;
  232.  
  233.             return h + ':' + m + ':' + s;
  234.         };
  235.  
  236.         // 객체 상속 함수
  237.         function extend() {
  238.  
  239.             var target = this
  240.           , opts = []
  241.           , src = null
  242.           , copy = null;
  243.  
  244.             for (var i = 0, length = arguments.length; i < length; i++) {
  245.  
  246.                 opts = arguments[i];
  247.  
  248.                 for (var n in opts) {
  249.                     src = target[n];
  250.                     copy = opts[n];
  251.  
  252.                     if (src === copy) continue;
  253.                     if (copy) target[n] = copy;
  254.                 }
  255.             }
  256.         };
  257.  
  258.  
  259.     })(window, document);
  260.  
  261.  
  262.     bind(window, 'load', function () {
  263.  
  264.         p1 = Video({
  265.             id: 'video_test1',
  266.             src: 'http://m.fpscamp.com/test.mp4',
  267.             poster: 'http://static.naver.net/www/u/2010/0611/nmms_215646753.gif',
  268.             width: document.documentElement.scrollWidth,
  269.             height: window.screen.height,
  270.             autoplay: false,
  271.             controls: true,
  272.             loop: false,
  273.             onload: function () {
  274.             },
  275.             onplay: function () {
  276.             },
  277.             onprogress: function () {
  278.             },
  279.             ontimeupdate: function () {
  280.             },
  281.             onended: function () {
  282.             },
  283.             onerror: function () {
  284.                 alert(this === window);
  285.             }
  286.         });
  287.     });
  288.    
  289.  
  290. // 이벤트 할당
  291. function bind(elem, type, handler, capture) {
  292.     type = typeof type === 'string' && type || '';
  293.     handler = handler || function () { ; };
  294.  
  295.     if (elem.addEventListener) {
  296.         elem.addEventListener(type, handler, capture);
  297.     }
  298.     else if (elem.attachEvent) {
  299.         elem.attachEvent('on' + type, handler);
  300.     }
  301.  
  302.     return elem;
  303. };
  304.  
  305. //]]>
  306. </script>
  307. <input id="" type="button" value="play" onclick="p1.play()" />
  308. <input id="" type="button" value="stop" onclick="p1.stop()" />
  309. <input id="" type="button" value="isEnded" onclick="alert(p1.isEnded())" />
  310. <input id="" type="button" value="getTimeout" onclick="alert(p1.getTimeout())" />
  311. <input id="" type="button" value="getStartTime" onclick="alert(p1.getStartTime())" />
  312. <input id="" type="button" value="getSrc" onclick="alert(p1.getSrc())" />
  313. <input id="" type="button" value="isBuffering" onclick="alert(p1.isBuffering())" />
  314. <input id="" type="button" value="getCurrentTimeout" onclick="alert(p1.getCurrentTimeout())" />
  315. <input id="" type="button" value="setVolume" onclick="p1.setVolume(5)" />
  316. <input id="" type="button" value="setMuted" onclick="p1.setMuted(true)" />
  317. <input id="Button1" type="button" value="getVideoSize(width)" onclick="alert(p1.getVideoSize().w)" />
  318. <input id="Button2" type="button" value="getVideoSize(height)" onclick="alert(p1.getVideoSize().h)" />
  319.  
  320. <div id="test"></div>
  321. </body>
  322. </html>



실행 페이지:





Video 객체 브라우저 지원 여부:





참고사이트:


레퍼런스:

http://www.w3schools.com/html5/tag_video.asp


브라우저 지원 여부

 http://caniuse.com/


@박종명님의 블로그


http://m.mkexdev.net/63