javascript写的一个MidiaPlayer的类

运行时候根据需求,改动这里即可

mpl.OpenUrl("DuskToDawn.wma");

下面是完整代码:

view plaincopy to clipboardprint?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>MediaPlayer示例</title> 
    <mce:style type="text/css"><!--  
        body  
        {  
            font-size:12px;  
            font-family:宋体;  
        }  
        #divMpl  
        {  
            height: 137px;  
            width: 277px;  
        }  
        #divMsg  
        {  
            height: 155px;  
            overflow:auto;  
            width: 376px;  
        }  
      
--></mce:style><style type="text/css" mce_bogus="1">        body  
        {  
            font-size:12px;  
            font-family:宋体;  
        }  
        #divMpl  
        {  
            height: 137px;  
            width: 277px;  
        }  
        #divMsg  
        {  
            height: 155px;  
            overflow:auto;  
            width: 376px;  
        }  
    </style> 
    <mce:script language="javascript" type="text/javascript"><!--  
    /* MediaPlayer类定义 冰点原创 */  
    function MediaPlayer()  
    {  
        this.dom=null;  
    }  
    MediaPlayer.uiMode=  
    {  
        Full:"full",  
        Mini:"mini",  
        None:"none",  
        Invisible:"invisible"  
    }  
    MediaPlayer.prototype={  
        CreateAt:function(id)   //在指定ID的标签中创建MediaPlayer控件,大小由该标签决定  
        {  
            this.dom=document.createElement("object");  
            this.dom.classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6";  
            this.dom.style.width="100%";  
            this.dom.style.height="100%";  
            var container=document.getElementById(id);  
            container.innerHTML="";  
            container.appendChild(this.dom);  
            this._Init();  
        },  
        BindID:function(id) //绑定一个已存在的Object标签,该标签应该为一个MediaPlayer控件  
        {  
            this.dom=document.getElementById(id);  
            this._Init();  
        },  
        _Init:function()    //初始化,注册事件  
        {  
            var _this=this;  
            this.dom.attachEvent("PlayStateChange",  
                function(newState){_this.onPlayStateChange(newState)});  
            this.dom.attachEvent("Buffering",function(bStart){ _this.onBuffering(bStart) });  
            this.dom.attachEvent("Error",function(){ _this.onError(); });  
            this.dom.attachEvent("PositionChange",  
                function(oldPos,newPos){ _this.onPositionChange(oldPos,newPos); } );  
            this.dom.attachEvent("StatusChange",function(){ _this.onStatusChange(); })  
        },  
        onPlayStateChange:function(newState)  
        {  
            switch(newState)  
            {  
            case 1: //wmppsStopped  
                this.onStop();  
                break;  
            case 2:  //wmppsPaused  
                this.onPaused();  
                break;  
            case 3:  //wmppsPlaying  
                this.onPlay();  
                break;  
            case 4:  //wmppsScanForward  
                break;  
            case 5:  //wmppsScanReverse  
                break;  
            case 6:  //wmppsBuffering  
                this.onBuffering_SC();  
                break;  
            case 7:  //wmppsWaiting  
                break;  
            case 8:  //wmppsMediaEnded  
                this.onMediaEnded();  
                break;  
            case 9:  //wmppsTransitioning  
                this.onTransitioning();  
                break;  
            case 10:  //wmppsReady  
                break;  
            case 11:  //wmppsReconnecting  
                break;  
            case 12:  //wmppsLast  
                break;  
            case 0:  //wmppsUndefined  
                break;  
            default:  
                break;  
            }  
        },  
        // 事件列表  
        onStop:function(){},  
        onPaused:function(){},  
        onPlay:function(){},  
        onBuffering_SC:function(){},  
        onTransitioning:function(){},  
        onMediaEnded:function(){},  
        onError:function(){},  
        onPositionChange:function(oldPos,newPos){},  
        onStatusChange:function(){},  
        onBuffering:function(bStart){},  
          
        // 设置 暂时只做两个  
        setMode:function(mode){ this.dom.uiMode=mode; },  
        setValume:function(v){ this.dom.settings.volume=v; },  
          
        // 各种属性  
        getMediaName:function()  
        {  
            var media=this.dom.currentMedia;  
            if(media)  
            {  
                return media.name;  
            }  
            return "";  
        },  
        getMediaDuration:function()  
        {  
            var media=this.dom.currentMedia;  
            if(media)  
            {  
                return media.duration;  
            }  
            return "";  
        },  
        getMediaDurationString:function()  
        {  
            var media=this.dom.currentMedia;  
            if(media)  
            {  
                return media.durationString;  
            }  
            return "";  
        },  
        getStatus:function(){ return this.dom.status; },  
        getPosition:function(){ return this.dom.controls.currentPosition; },  
        getPositionString:function(){ return this.dom.controls.currentPositionString; },  
        getPlayState:function(){ return this.dom.playState; },  
          
        // 操作方法  
        OpenUrl:function(URL){ this.dom.URL=URL; },  
        Play:function(){ this.dom.controls.play(); },  
        Pause:function(){ this.dom.controls.pause();},  
        Stop:function(){ this.dom.controls.stop(); }  
    }  
    /* MediaPlayer类定义 冰点原创 */  
      
    var mpl=new MediaPlayer();  //创建一个MediaPlayer  
    window.οnlοad=function()  
    {  
        mpl.CreateAt("divMpl");  
        mpl.setValume(100);  
        mpl.setMode(MediaPlayer.uiMode.Full);  
        mpl.onPlay=function(){ ShowMessage("正在播放["+this.getMediaName()+"]"); };  
        mpl.onPaused=function(){ ShowMessage("暂停"); };  
        mpl.onMediaEnded=function(){ ShowMessage("播放结束"); };  
        mpl.onStop=function(){ ShowMessage("停止"); };  
        mpl.onPositionChange=function(oldPos,newPos){  
            var pos1={  
                min:parseInt(oldPos/60),  
                sec:parseInt(oldPos%60)  
            }  
            var pos2={  
                min:parseInt(newPos/60),  
                sec:parseInt(newPos%60)  
            }  
            ShowMessage(pos1.min+":"+pos1.sec+ "->"+ pos2.min+":"+pos2.sec);  
        };  
        //mpl.onStatusChange=function(){ ShowMessage(this.getStatus()); };  
        mpl.OpenUrl("DuskToDawn.wma");  
        window.setInterval("ShowPlayTime()",1000);  
    }  
    function ShowPlayTime()  
    {  
        if(mpl.getPlayState()==3)  
        {  
            ShowStatus(mpl.getPositionString());  
        }  
    }  
    function ShowMessage(str)  
    {  
        var msg=document.getElementById("divMsg");  
        var tn=document.createTextNode(str);  
        msg.appendChild(tn);  
        msg.appendChild(document.createElement("br"));  
    }  
    function ShowStatus(str)  
    {  
        document.getElementById("divStatus").innerHTML=str;  
    }  
      
// --></mce:script> 
</head> 
<body> 
<div id="divMpl"></div> 
<div id="divStatus"> </div> 
    <input type="button" value="清空消息" οnclick="document.getElementById('divMsg').innerHTML='';" /> 
<div id="divMsg"></div> 
</body> 
</html> 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lihan6415151528/archive/2009/04/25/4121624.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值