<?php
// (c) 2009 Michael A. Peters - all rights reserved
class embedMedia {
   
// default dimensions are what I like for mp3 playback.
   
public $width='384';
   public 
$height='24';
   public 
$autoplay false;
   public 
$autobuffer false;
   public 
$controls true;
   public 
$type 'audio';
   public 
$poster '';
   public 
$fullscreen false;
   
// change these to your environment (extend class)
   
public $fplayer  '/swf/flowplayer-3.1.3.swf';
   public 
$faplayer '/swf/flowplayer.audio-3.1.2.swf';
   
// leave if you don't have commercial
   
public $fpkey    '';

   public function 
embedMedia($dom,$id,$mpg,$ogg="") {
      
$this->dom $dom;
      
$this->id  $id;
      
$this->mpg $mpg;
      
$this->ogg $ogg;
      }
      
   public function 
html5() {
      
$fallback $this->hyperLink();
      
$html     $this->html5media();
      
$flash    $this->flowPlay();
      
$flash->appendChild($fallback);
      
$html->appendChild($flash);
      return(
$html);
      }
      
   public function 
flow() {
      
$fallback $this->hyperLink();
      
$html     $this->html5media();
      
$flash    $this->flowPlay();
      
$html->appendChild($fallback);
      
$flash->appendChild($html);
      return(
$flash);
      }

   private function 
html5media() {
      if (
strcmp($this->type,'video') == 0) {
         
$mimeMPG 'video/mp4';
         
$mimeOGG 'video/ogg';
         } else {
         
$mimeMPG 'audio/mpeg';
         
$mimeOGG 'audio/ogg';
         }
      
$media $this->dom->createElement($this->type);
      
$media->setAttribute('id','h5_' $this->id);
      if (
$this->controls) {
         
$media->setAttribute('controls','controls');
         }
      if (
$this->autoplay) {
         
$media->setAttribute('autoplay','autoplay');
         }
      if (
$this->autobuffer) {
         
$media->setAttribute('autobuffer','autobuffer');
         }
      if (
strcmp($this->type,'video') == 0) {
         
$media->setAttribute('width',$this->width);
         
$media->setAttribute('height',$this->height);
         if (
strlen($this->poster) > 0) {
            
$media->setAttribute('poster',$this->poster);
            }
         }

      
$source $this->dom->createElement('source');
      
$source->setAttribute('src',$this->mpg);
      
$source->setAttribute('type',$mimeMPG);
      
$media->appendChild($source);   
   
      if (
strlen($this->ogg) > 0) {
         
// add ogg source
         
$source $this->dom->createElement('source');
         
$source->setAttribute('src',$this->ogg);
         
$source->setAttribute('type',$mimeOGG);
         
$media->appendChild($source);
         }
      return(
$media);
      }
      
   private function 
flowPlay() {
      
$object $this->dom->createElement('object');
      
$object->setAttribute('id','fp_' $this->id);
      
$object->setAttribute('width',$this->width);
      
$object->setAttribute('height',$this->height);
      
$object->setAttribute('data',$this->fplayer);
      
$object->setAttribute('type','application/x-shockwave-flash');
      
      
$param $this->dom->createElement('param');
      
$param->setAttribute('name','movie');
      
$param->setAttribute('value',$this->fplayer);
      
$object->appendChild($param);
      
      
$param $this->dom->createElement('param');
      
$param->setAttribute('name','allowfullscreen');
      
$param->setAttribute('value','true');
      
$object->appendChild($param);
      
      
$param $this->dom->createElement('param');
      
$param->setAttribute('name','allowscriptaccess');
      
$param->setAttribute('value','always');
      
$object->appendChild($param);
      
      
$param $this->dom->createElement('param');
      
$param->setAttribute('name','flashvars');
      
$param->setAttribute('value',$this->buildFlashvars());
      
$object->appendChild($param);
      return(
$object);
      }
      
   private function 
hyperLink() {
      
$fallback $this->dom->createElement('p');
      
$link $this->dom->createElement('a',basename($this->mpg));
      
$link->setAttribute('href',$this->mpg);
      
$fallback->appendChild($link);
      if (
strlen($this->ogg) > 0) {
         
$br $this->dom->createElement('br');
         
$fallback->appendChild($br);
         
$link $this->dom->createElement('a',basename($this->ogg));
         
$link->setAttribute('href',$this->ogg);
         
$fallback->appendChild($link);
         }
      return(
$fallback);
      }
      
   private function 
buildFlashvars() {
      
$string "config=";
      if (
strlen($this->fpkey) > 0) {
         
$json['key']='$07cb470b94495243b2c';
         }
      
// build the playlist
      
if (strlen($this->poster) > 0) {
         
$poster['url']=$this->poster;
         
$playlist[]=$poster;
         }
      
$multimedia['url']=$this->mpg;
      if (! 
$this->autoplay) {
         
$multimedia['autoPlay']=false;
         }
      if (
$this->autobuffer) {
         
$multimedia['autoBuffering']=true;
         }
      
$playlist[]=$multimedia;
      
$json['playlist']=$playlist;
      
// plugins
      
if (strcmp($this->type,'audio') == 0) {
         
$audio['url'] = $this->faplayer;
         
$plugins['audio']=$audio;
         
$controls['height']=$this->height;
         }
      if (! 
$this->fullscreen) {
         
$controls['fullscreen']=false;
         }
      if (isset(
$controls)) {
         
$plugins['controls']=$controls;
         }
      if (isset(
$plugins)) {
         
$json['plugins']=$plugins;
         }
      
// end of plugins
      
$string .= json_encode($json);
      
$string preg_replace('/"/','\'',$string);
      
$string preg_replace('/\\\\/','',$string);
      
//die($string);
      
return($string);
      }
   } 
// end of class
?>