src/js/popcorn.jwplayer.js
branchpopcorn-port
changeset 51 1444edeae73f
child 54 bbcf7423f543
equal deleted inserted replaced
50:b5849024c5c5 51:1444edeae73f
       
     1 var jwplayerObjects = {};
       
     2 
       
     3 Popcorn.player( "jwplayer", {
       
     4   _setup: function( options ) {
       
     5 
       
     6     var media = this,
       
     7         player = {},
       
     8         container = document.createElement( "div" ),
       
     9         currentTime = 0,
       
    10         seekTime = 0,
       
    11         seeking = false,
       
    12         dataLoaded = false;
       
    13     container.id = media.id + Popcorn.guid();
       
    14 
       
    15     media.appendChild( container );
       
    16 
       
    17   var initApi = function () {
       
    18     jwplayer( container.id ).onTime(function() {
       
    19         currentTime = jwplayer(container.id).getPosition();
       
    20         media.dispatchEvent( "timeupdate" );
       
    21        // timeout = setTimeout( timeupdate, 10 );
       
    22     });
       
    23     
       
    24     media.play = function() {
       
    25       media.paused = false;
       
    26       media.dispatchEvent( "play" );
       
    27       if ( dataLoaded ) {
       
    28         media.dispatchEvent( "loadeddata" );
       
    29         dataLoaded = false;
       
    30       }
       
    31 
       
    32       media.dispatchEvent( "playing" );
       
    33       jwplayer( container.id ).play();
       
    34     };
       
    35     
       
    36     media.pause = function() {
       
    37 
       
    38       if ( !media.paused ) {
       
    39         media.paused = true;
       
    40         media.dispatchEvent( "pause" );
       
    41         jwplayer( container.id ).pause();
       
    42       }
       
    43     };
       
    44 
       
    45     Popcorn.player.defineProperty( media, "currentTime", {
       
    46           set: function( val ) {
       
    47             // make sure val is a number
       
    48             currentTime = seekTime = +val;
       
    49             seeking = true;
       
    50             media.dispatchEvent( "seeked" );
       
    51             media.dispatchEvent( "timeupdate" );
       
    52             jwplayer( container.id ).seek( currentTime );
       
    53             return currentTime;
       
    54           },
       
    55           get: function() {
       
    56             return jwplayer( container.id ).getPosition();            
       
    57           }
       
    58         });
       
    59  
       
    60     Popcorn.player.defineProperty( media, "muted", {   
       
    61         set: function( val ) {
       
    62           if ( jwplayer( container.id ).getMute() !== val ) {
       
    63             if ( val ) {
       
    64               jwplayer( container.id ).setMute(true);
       
    65             } else {
       
    66               jwplayer( container.id ).setMute(false);
       
    67             }
       
    68 
       
    69             media.dispatchEvent( "volumechange" );
       
    70           }
       
    71           
       
    72           return jwplayer( container.id ).getMute();
       
    73         },
       
    74         get: function() {
       
    75           return jwplayer( container.id ).getMute();
       
    76         }
       
    77     });
       
    78   
       
    79     Popcorn.player.defineProperty( media, "volume", {
       
    80     
       
    81       set: function( val ) {
       
    82 
       
    83         if ( jwplayer( container.id ).getVolume() !== val *100 ) {
       
    84           jwplayer( container.id ).setVolume( val * 100);
       
    85           media.dispatchEvent( "volumechange" );
       
    86         }
       
    87         
       
    88         return (jwplayer( container.id ).getVolume()) / 100;
       
    89       },
       
    90       
       
    91       get: function() {
       
    92         return jwplayer( container.id ).getVolume() / 100;
       
    93       }
       
    94     });
       
    95 
       
    96     media.readyState = 4;
       
    97     media.dispatchEvent( 'load' );
       
    98     dataLoaded = true;
       
    99 
       
   100     media.duration = options.duration;
       
   101     media.dispatchEvent( 'durationchange' );
       
   102 
       
   103     media.paused && media.dispatchEvent( 'loadeddata' );
       
   104 
       
   105     };
       
   106 
       
   107     jwplayer( container.id ).setup({
       
   108       file: options.file,
       
   109       height: 270,
       
   110       width: 480,
       
   111       flashplayer: options.flashplayer,
       
   112       events: {
       
   113         onReady: initApi
       
   114       }});
       
   115 
       
   116   }
       
   117 });
       
   118