Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Windows 綜合教學 >> Windows 技巧

網頁控制 Windows Media Player 指定區段播放:ASX 檔案實作

編輯:Windows 技巧

專案需求:使用者輸入開始/結束時間,Media Player 只播放該區段。經研究發現,直接操控 ActiveX 控制項屬性(CurrentPosition, SelectionStart 等)在本機可行,但部署至遠端伺服器時,緩衝未完成前無法跳轉,導致功能失效。

解決方案:使用 ASX 中繼檔

Windows Media Metafile(.asx)支援 <STARTTIME> 與 <DURATION> 標籤,精確控制播放切入點與長度,且不受緩衝限制。

ASX 範本

<ASX VERSION="3.0">
  <TITLE>你的標題</TITLE>
  <ENTRY>
    <REF HREF="音訊檔案完整URL" />
    <STARTTIME VALUE="0:30.0" />   <!-- 從 30 秒開始 -->
    <DURATION VALUE="00:50" />      <!-- 播放 50 秒 -->
  </ENTRY>
</ASX>

實作流程

  1. 前端收集使用者輸入的開始/結束時間。
  2. 後端動態產生對應的 .asx 檔案(或前端 JS 組 Blob 產生 Object URL)。
  3. 將 .asx 檔案 URL 傳給頁面上的 Media Player ActiveX / <object>。

播放器參數關鍵設定

經測試,僅保留以下參數最穩定:

<param name="AutoStart" value="true">
<param name="Filename" value="">       <!-- 由 JS 動態指定 .asx URL -->
<param name="ShowPositionControls" value="true">
<param name="ShowStatusBar" value="true">

關鍵發現: 設 AutoStart="false" 會導致 .asx 無法播放,必須設為 true;Filename 初始留空,由腳本動態賦值。

完整前端範例(概念)

// 使用者送出後
const asxContent = `...`; // 組裝 ASX 字串
const blob = new Blob([asxContent], {type: 'video/x-ms-asx'});
const asxUrl = URL.createObjectURL(blob);
player.Filename = asxUrl; // player 為 Media Player 物件參考

此法完美解決遠端指定區段播放問題,適用於試聽、教學切片等場景。

Copyright © Windows教程網 All Rights Reserved