rails开发利器:视频播放插件plugin(如何开发一个简单的插件)

本文介绍了如何在Rails 2.3.8环境下开发一个名为video_player的插件,包括创建helper来展示Flash视频播放器,区分直播和非直播模式,并通过rake任务自动复制资源文件到Public目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1.  For: rails2.3.8  
    因为我的项目是基于rails2.3.8的,以后在做rails3.0的
  2. plugin的名称是 video_player, 新建plugin
    ruby  script/generate plugin video_player --with-generator

  3. 重新组织我的文件目录
    新建video_player和rails文件夹,新建rails/init.rb文件
    mkdir rails
    mkdir lib/video_player
    touch rails/init.rb

    修改 rails/init.rb
    require 'video_player'

    删除多余的文件
     rm -rf init.rb install.rb
    
  4. 整理一下我的思路:
    4.1 我需要新建一个helper,用于显示flash video, 而且能分直播和非直播的模式
    4.2 我需要新建一个rake task,用于资源的复制
  5. 开发helper方法
    新建lib/app/helpers/video_player_helper.rb文件
    module VideoPlayerHelper
    
    
       def video_player(url_bad='',live=true,width=500, height=400)
        flash_url_bad = "http://#{request.env["HTTP_HOST"]}/player.swf"
        flash_url = flash_url_bad
        player = "<object width='#{width}' height='#{height}'>"
        player += '<param value="transparent" name="wmode"></param>'
        player +=  "<param value='#{flash_url}' name='movie'></param>"
        url = URI.escape(url_bad, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
        if live
          player += "<param value='src=#{url}&streamType=live&autoRewind=true&autoPlay=true&bufferingOverlay=false&autoDynamicStreamSwitch=false&optimizeInitialIndex=false&bufferTime=0&initialBufferTime=0&expandedBufferTime=0&dynamicStreamBufferTime=0&dvrBufferTime=0&dvrDynamicStreamingBufferTime=0&liveBufferTime=0&liveDynamicStreamingBufferTime=0&minContinuousPlaybackTime=0' name='flashvars'> </param> "
        else
          player += "<param value='src=#{url}&autoRewind=true&autoPlay=true' name='flashvars'> </param>"
        end
    
        player += "<param value='true' name='allowFullScreen'></param>
                <param value='always' name='allowscriptaccess'></param>
                <embed width='#{width}' height='#{height}' wmode='transparent'"
        if live
          player += "flashvars='src=#{url}&streamType=live&autoRewind=true&autoPlay=true&bufferingOverlay=false&autoDynamicStreamSwitch=false&optimizeInitialIndex=false&bufferTime=0&initialBufferTime=0&expandedBufferTime=0&dynamicStreamBufferTime=0&dvrBufferTime=0&dvrDynamicStreamingBufferTime=0&liveBufferTime=0&liveDynamicStreamingBufferTime=0&minContinuousPlaybackTime=0'"
        else
          player += "flashvars='src=#{url}&autoRewind=true&autoPlay=true'"
        end
    
        player += " allowfullscreen='true'  allowscriptaccess='always'  type='application/x-shockwave-flash'
            src='#{flash_url}'> </embed></object>"
        return player
      end
    
    end

    修改video_player.rb文件,将helper加入application中
    %w{ helpers }.each do |dir|
    
      path = File.join(File.dirname(__FILE__), 'app', dir)
      $LOAD_PATH << path
      ActiveSupport::Dependencies.load_paths << path
      ActiveSupport::Dependencies.load_once_paths.delete(path)
    end 
    
    if defined?(ActionView::Base)
      ActionView::Base.send :include, VideoPlayerHelper
    end


  6. 新建一个rake,用于复制player.swf到Public目录
    require 'fileutils'
    include FileUtils::Verbose
    desc "copy player.swf to public dir"
    task :copy_video_player do
      play_swf = File.join(File.dirname(__FILE__),"player.swf")
      cp(play_swf, File.join(RAILS_ROOT, "public"))
    end
  7. 代码地址
    https://github.com/chucai/videoplayer


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值