1. 程式人生 > >使用JS與MediaPlayer編寫網頁播放器

使用JS與MediaPlayer編寫網頁播放器

剛開始我使用的是新版本9.0以後的mediaplayer外掛,新版本的外掛有很多功能、屬性、方法和事件處理功能,並且最簡單的是使用它的playlist功能控制播放器的播放,不過由於有許可權的問題,這種方法在本機上可行,上傳到伺服器並且用其他的電腦訪問則播放不了,提示“沒有許可權”,錯誤在這方法上appendItem,因為一般playlist的許可權是隻讀的,不能建立或者修改,只能自己去設計一個播放列表,自己去修改url屬性去選曲。所以放棄這種方法改用其他方法。

參考官方的SDK資訊,有很多,我只是截取了一部分如下:

Media Library Access
Properties and methods of the Windows Media Player object model that access Media Library require either read-only or read/write access to the database. Media Library contains information that some users want to keep private and that should only be accessed or altered with their consent.

To determine the current level of access granted to your code, retrieve the Settings.mediaAccessRights property. That property returns "none", "read", or "full" (read/write). To request specific access rights, call the Settings.requestMediaAccessRights method, passing a parameter that specifies the level you are requesting. The method displays a message to the user explaining the requested level of access, and returns a Boolean value indicating whether the access was granted.

Certain access rights are granted automatically depending on where your code is running relative to the user’s computer.

If your Web page or program is located on the user’s computer, full access rights are granted by default.
Web pages have read access to Player.currentMedia, Player.currentPlaylist, and Media.sourceURL when the Web page is located in an Internet Explorer security zone that is the same as or less restricted than the security zone of the media item or playlist.
Ranging from least restricted to most restricted, the security zones are the Trusted zone (including the user’s local computer), the Local intranet zone, the Internet zone, and the Restricted zone.

For example, a Web page in the Local intranet zone has full access rights to Player.currentMedia when the corresponding media item is on the local intranet or the Internet, but access rights must be requested for media items located on a user’s local computer or on a Web site in the Trusted zone.

For Netscape Navigator, the Java applet does not have any automatic access to Media Library. However, you can request the access rights you need for your application. For general information, see Using Windows Media Player with Netscape Navigator.
You should test your Web-based or Windows-based application in all of the security zones it may encounter. The application should be designed to handle denial of an access request correctly.

Windows Media Player object model versions prior to Windows Media Player 9 Series do not include mediaAccessRights or requestMediaAccessRights.

對於9.0以後的版本可以用事件監聽的方法,對playstate為8的時候進行處理,相關狀態說明請還是參考9.0的SDK。

<SCRIPT FOR="Player" EVENT="PlayStateChange(NewState)" LANGUAGE="javascript">
switch(NewState)
{
    //已停止,那麼播放下一首
    case 1:
        //Next();
        break;
       
    //歌曲播放完畢,那麼做判斷重複播放還是播放下一首
    case 8:
        switch(play_mode)
        {
            case ‘loop’:
                Next();
                break;
           
            case ‘random’:
                var random_index=Math.floor(Math.random()*length);
                PlayThis(random_index);
                break;       
   
            case ‘single’:
                PlayThis(current_index);
                break;
        }
        break;
   
    default:
        //alert(‘新的狀態: ‘+NewState);
        break;
}
</SCRIPT>

但是對於老版本的6.4沒有事件功能,所以必須對播放歌曲的時間長度進行監聽,

function Playing()
{
    //判斷無法正常播放或者是否播放完成
    if((waited>2)&&Player.CurrentPosition>=(Player.Duration-1))
    {
        //重置等待時間
        waited=0;
       
        switch(play_mode)
        {
            case ‘loop’:
                Next();
                break;
           
            case ‘random’:
                var random_index=Math.floor(Math.random()*length);
                PlayThis(random_index);
                break;       
   
            case ‘single’:
                PlayThis(current_index);
                break;
        }
    }
    else if(Player.Duration==0)
    {
        waited+=1;
    }
    else if(Player.Duration)
    {
        waited=0;
    }

    setTimeout(‘Playing()’,1000);
}

/*初始化播放器*/
function IniPlayer()
{
    // 取得當前的播放列表
    while($(‘#p’+length).find(‘input’).val()){
        temp_str=$(‘#p’+length).find(‘input’).val();
        list_array[length]=temp_str.split(‘|’);
        length++;
    }
   
    SetPlayMode(‘loop’);
    PlayThis(0);
   
    //進入迴圈監聽
    Playing();
}

$(document).ready(function(){
    IniPlayer();
});

迴圈監聽!!

基本上就是這樣了,前些時候採用那個playlist進行播放控制,結果上傳到伺服器,其他電腦訪問我的電腦都無法播放,“沒有許可權”問題把我弄的挺鬱悶的。

為了相容不同瀏覽器,你要把wmp只當作播放來使用,不要使用其它功能,然後抽象出介面,再根據不同播放器實現功能,這個是多數js播放器的做法。。