- For: rails2.3.8
因为我的项目是基于rails2.3.8的,以后在做rails3.0的 - plugin的名称是 video_player, 新建plugin
ruby script/generate plugin video_player --with-generator
- 重新组织我的文件目录
新建video_player和rails文件夹,新建rails/init.rb文件
mkdir rails mkdir lib/video_player touch rails/init.rb
修改 rails/init.rbrequire 'video_player'
删除多余的文件rm -rf init.rb install.rb
- 整理一下我的思路:
4.1 我需要新建一个helper,用于显示flash video, 而且能分直播和非直播的模式
4.2 我需要新建一个rake task,用于资源的复制 - 开发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
- 新建一个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
- 代码地址
https://github.com/chucai/videoplayer