使用Require.js加载jwplayer.js [英] Loading jwplayer.js using Require.js

查看:83
本文介绍了使用Require.js加载jwplayer.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我是Require.js的新手,并且我已经通过使用Require.js方法加载各种其他库来学习该库.

So, I'm new to Require.js and I've been learning this library by loading various other libraries using Require.js methods.

我已经成功加载了Knockout.js对象,Chart.js对象以及自定义的Require.js定义的对象.

I've successfully load Knockout.js objects, Chart.js object, as well as custom Require.js defined objects.

但是我似乎无法使用Require.js加载jwplayer.这是我收到的错误方法: 未捕获的TypeError:无法调用未定义的方法'jwplayer'

But I can't seem able to load jwplayer using Require.js. This is the error method I received: Uncaught TypeError: Cannot call method 'jwplayer' of undefined

这是我的示例代码(Knockout,Chart对象都已成功加载)

This is my sample code (the Knockout, Chart objects all loaded successfully)

require(['jwplayer/jwplayer', 'libs/Chart', 'libs/knockout-2.1.0', 'appViewModel','helper/util'], function(jwplayer, chart, ko, appViewModel, util) {

//LOADING FROM jwplayer.js
jwplayer("player").setup({
    width: '320',
    height: '40',
    sources: [{
        file: "rtmp://127.0.0.1:1935/vod/mp3:sample_1.mp3"
    },{
        file: "http://127.0.0.1:1935/vod/sample_1.mp3/playlist.m3u8"
    }]
});

//LOADING FROM Chart.js
var barChartData = {
    labels : ["January","February","March","April","May","June","July"],
    datasets : [
        {
            fillColor : "rgba(220,220,220,0.5)",
            strokeColor : "rgba(220,220,220,1)",
            data : [65,59,90,81,56,55,40]
        },
        {
            fillColor : "rgba(151,187,205,0.5)",
            strokeColor : "rgba(151,187,205,1)",
            data : [28,48,40,19,96,27,100]
        }
    ]   
};
var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Bar(barChartData);

//LOADING FROM knockout-2.1.0.js
ko.applyBindings(new appViewModel());

//LOADING FROM A CUSTOM DEFINED OBJECT
util.greets(); 
    });

那么如何使用Require.js加载jwplayer.js?

So how do you load jwplayer.js using Require.js?

推荐答案

jwplayer.js没有为require.js定义模块,因此您将不得不使用

jwplayer.js doesn't define a module for require.js, so you're going to have to use the shim config, something like this:

require.config({
    shim: {
        'jwplayer/jwplayer': {
            exports: 'jwplayer'
        }
    }
});

您可以在requirejs api文档中看到有关如何使用它的更多信息.

You can see more about how to use it in the requirejs api docs.

代码示例中的错字.

应该注意的是,如果jwplayer()找不到通过它的播放器,它将返回null,因此,即使正确加载了播放器,它仍然会抛出该错误.如果甚至在包含配置后仍然出现错误,请尝试添加类似

Edit 2: it should be noted that jwplayer() will return null if it can't find the player that you pass it, so even if it is loaded correctly, it will still throw that error. If you're getting the error even after including the configuration, try adding something like

console.log(jwplayer.api);

在require回调中,并检查您的控制台以查看是否有任何内容.

in the require callback and check your console to see if there's anything there.

这篇关于使用Require.js加载jwplayer.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆