📜 ⬆️ ⬇️

Yii extension for displaying MP4 on the knee

We are preparing a project on Yii, in which users can, among other things, post and view small materials in MP4 format. They suggested several solutions regarding the player, I decided to stop at JW Player. Tests it was successful. It remains to quickly write the appropriate extension for Yii.
Under the cut, step by step, where from where to download and listing the extension code with comments.


I admit at once that this is my first experience not only writing extensions under Yii, but also generally working with this framework.

So. First, go to longtailvideo.com and download the JW Player . Unpack the archive. From there we will need files: jwplayer.js, swfobject.js and player.swf. Create a directory jwplayer in the directory with the extensions of your Yii project (that is, protected / extensions) (in this way our extension will be called), and in turn create the assets directory. We place 3 files from the archive into it (i.e. the full path to where jwplayer.js, swfobject.js and player.swf should be placed is protected / extensions / jwplayer / assets).
')
Now we proceed directly to creating the expansion itself. Create a JWplayer.php file in the protected / extensions / jwplayer directory. In it we will describe our class for our widget.

Further listing of a code of this file with comments.

<?php /* * JWplayer widget class file. * JWplayer extends CWidget * @version 1.0 - 20110516 * @author skarah <skarah@mail.ru> * @copyright Copyright © 2011 skarah * @license New BSD License http://www.opensource.org/licenses/bsd-license.php */ class JWplayer extends CWidget { /** *     -. * * @var array */ public $_options = array( // ID  'id' => 'playerID', //     'width' => 480, //     'height' => 270, //    'file' => '', //    - 'flashplayer' => '', ); /** *     options. * * @var array */ public $options = array(); /** *   assets. */ public function init() { $this->publishAssets(); } /** *      */ public function run() { /* *         */ extract($this->_options); /* *    */ extract($this->options); echo "<div id='mediaplayer'></div> <script type=\"text/javascript\"> jwplayer('mediaplayer').setup({ 'id': '{$id}', 'width': '{$width}', 'height': '{$height}', 'file': '{$file}', 'flashplayer': '{$flashplayer}', 'modes': [{type: 'flash', src: '{$flashplayer}'}] }); </script>"; } protected function publishAssets() { /* *   assets */ $assets = dirname(__FILE__) . '/assets'; $baseUrl = Yii::app()->assetManager->publish($assets); if (is_dir($assets)) { /* *      - */ $this->_options['flashplayer'] = $baseUrl . '/player.swf'; /* *  js   */ Yii::app()->clientScript->registerScriptFile($baseUrl . '/jwplayer.js', CClientScript::POS_HEAD); Yii::app()->clientScript->registerScriptFile($baseUrl . '/swfobject.js', CClientScript::POS_HEAD); } else { throw new Exception(' JWplayer:     assets  .'); } } } 


All is ready. It is possible in the submission or in the form (this is someone who likes to call it) launch our video widget, for this it will be enough to insert the following code:
 $this->widget('application.extensions.jwplayer.JWplayer', array( 'options' => array( 'file' => '/path_to/my_mp4file.mp4' ), )); 


Look like that's it. Thanks for attention.

Source: https://habr.com/ru/post/119368/


All Articles