CMP4使用手册 - 使用范例

CMP4插入到页面

CMP就是个flash,可以使用插入flash的通用方式嵌入到网页中。比如:直接使用html的object标签,或者使用流行的swfobject
但是CMP4还有一些特殊功能和应用,为此提供了CMP专用嵌入脚本:cmp.js,以下就是一些CMP嵌入的例子
直接在当前位置写入,最简方式:
<script type="text/javascript" src="http://cenfunmusicplayer.googlecode.com/svn/trunk/js/cmp.js"></script>
<script type="text/javascript">
CMP.write("cmp", "600", "400", "cmp.swf");
</script>
在指定容器中插入,易于扩展:
<div id="player" style="width:600px;height:400px;"></div>
<script type="text/javascript" src="http://cenfunmusicplayer.googlecode.com/svn/trunk/js/cmp.js"></script>
<script type="text/javascript">
var htm = CMP.create("cmp", "100%", "100%", "cmp.swf");
document.getElementById("player").innerHTML = htm;
</script>
插入的同时传递一些参数:
<div id="player" style="width:600px;height:400px;"></div>
<script type="text/javascript" src="http://cenfunmusicplayer.googlecode.com/svn/trunk/js/cmp.js"></script>
<script type="text/javascript">
var flashvars = {
name : "CMP4",
skin : "skins/vplayer.zip",
lists : "mylist.xml"
};
var htm = CMP.create("cmp", "100%", "100%", "cmp.swf", flashvars);
document.getElementById("player").innerHTML = htm;
</script>
其中flashvars参数也支持字符串的形式(注意值需要URL转义),如:
var flashvars = "name=CMP4&skin=skins/vplayer.zip&lists=mylist.xml";
支持页面JS脚本回调的更全面方式:
<div id="player" style="width:600px;height:400px;"></div>
<script type="text/javascript" src="http://cenfunmusicplayer.googlecode.com/svn/trunk/js/cmp.js"></script>
<script type="text/javascript">
var cmpo;
function cmp_loaded(key) {
cmpo = CMP.get("cmp");
if (cmpo) {
document.title = cmpo.config("name");
cmpo.addEventListener("model_load", "cmp_model_load");
}
}
function cmp_model_load(data) {
document.title = cmpo.item("label");
}
var flashvars = {
name : "CMP4",
skin : "skins/vplayer.zip",
lists : "mylist.xml",
api : "cmp_loaded"
};
var htm = CMP.create("cmp", "100%", "100%", "cmp.swf", flashvars);
document.getElementById("player").innerHTML = htm;
</script>
只有回调cmp_loaded函数然后获取CMP所在flash的cmpo对象后,才可以进行JavaScript API通讯
更多高级开发的例子见:http://cenfunmusicplayer.googlecode.com/svn/trunk/cmp4/developer/