JW-Player and Rails 3.2

2020-02-14 08:44发布

I'm trying to use JW-Player in my application. Researching the issue a bit, there seems to be several abandoned efforts to produce a gem, and the latest is undocumented. So, here's how I'm going about it:

I downloaded the JW-Player version 6, unzipped and copied the files in my /app/assets/javascripts directory as follows:

app/assets/javascripts/jwplayer/jwplayer.js
app/assets/javascripts/jwplayer.html5.js
app/assets/javascripts/jwplayer.flash.swf

In my app/views/layouts/application.html.erb, I have the following:

<head>
    <%= javascript_include_tag "/assets/javascripts/jwplayer/" %>
</head>

and in app/views/pages/about.html.erb, I have the following:

<%= jw_player("http://xxxxx/video.mp4",
            :width => 200, :height => 110) %>

Here's what happens when I click on the About page link:

Showing xxxxxxxx/app/views/pages/about.html.erb where line #10 raised:

undefined method `jw_player' for #<#<Class:0x007fe77e37c018>:0x007fe780c1f678>

First time user of JW-Player.

7条回答
戒情不戒烟
2楼-- · 2020-02-14 09:14

This worked for me:

  • Place jwplayer folder in public (Downloaded from longtail video)
  • Include it like an external script, without using asset pipeline (HAML).

    %script{:src => '/jwplayer/jwplayer.js'}
    
  • In your video partial (ERB)

    <script type="text/javascript">
    jwplayer.key="Your key here";
    $(document).ready(function(){
        jwplayer("video").setup({
            height: 360,
            width: 640,
            playlist: [
            <% videos.each do |v| %>
                {
                    image: "<%= v.poster %>",
                    sources: [
                        { file: "<%= v.url %>" },
                    ]
                },
            <% end %>
            ]
        });
     })
    </script>
    
    <video id="video">Video Loading... Ensure JavaScript is enabled...</video>
    
查看更多
登录 后发表回答