目標。我在 Visual Studio 2019 中創建了一個 9-Form 應用程式,我正在嘗試以我的一種形式播放視頻檔案,該檔案與我的 winforms 應用程式的可執行檔案 (.exe) 存盤在同一檔案夾中。
腳步。我通過“COM 組件”安裝了 Windows Media Player 功能,并按原樣顯示在我的工具箱中(盡管圖示沒有出現在工具箱內的標題旁邊)。之后,我將“n”拖放到我的表單中,以便根據 Microsoft 檔案撰寫以下代碼。
private void button1_Click(object sender, System.EventArgs e)
{
axWindowsMediaPlayer1.URL = "video delphi.mp4";
}
實際結果。雖然當我在我的表單中拖動播放器時,它應該顯示出來,但是當我運行應用程式時,它變為空白,因為它從未被放置在那里或被拖動/使用在我的表單中。我已經檢查了播放器可見并啟用的屬性。在某些時候,通過按下播放按鈕,我只能聽到音頻而沒有視頻。
收到錯誤/通知。1. 當我第一次嘗試查看發生了什么時,我在我的應用程式運行時收到了一個通知,該檔案不受支持,盡管我沒有顯示 (.mp4) 擴展名,但它只顯示 (,)象征。該通知不是來自 VS 2019,而是來自播放器本身。2. 目前我收到 C00D1179 錯誤,而上述通知不再出現。
uj5u.com熱心網友回復:
為了使用 Windows Media Player 播放 .mp4 檔案,請嘗試以下已經過測驗的方法。
先決條件:如果尚未安裝,則必須安裝“mp4”編解碼器 - 例如
單擊Windows 表單應用程式 (.NET Framework)

單擊下一步
輸入所需的專案名稱(例如:MediaPlayerTest)
點擊創建
添加參考
- 在 VS 選單中,單擊專案
- 選擇添加參考...
- 單擊COM
- 選擇Windows 媒體播放器
- 單擊確定
打開解決方案資源管理器:
- 在 VS 選單中,單擊查看
- 選擇解決方案資源管理器
在設計器中打開 Form1
- 在解決方案資源管理器中,雙擊Form1.cs
打開工具箱:
- 在 VS 選單中,單擊查看
- 選擇工具箱
- 搜索:Windows 媒體播放器
- 如果未找到 Windows Media Player,請將其添加到工具箱中。
將 Windows Media Player 添加到工具箱(如果工具箱中尚不存在)
- 右鍵單擊所有 Windows 表單
- 選擇選擇專案...。
- 加載完成后,單擊COM Components。
- 選中“Windows Media Player**。

- 單擊確定
- Windows Media Player 現在應該存在于工具箱中的“所有 Windows 表單”下。
將 Windows Media Player 添加到 Form1
- 將 Windows Media Player 從工具箱拖到 Form1 上。
將按鈕添加到 Form1
- 將兩個按鈕從工具箱拖到 Form1 上
- 將按鈕 1 重命名為
btnPlay - 將按鈕 2 重命名為
btnStop - 雙擊
btnPlay創建單擊事件處理程式 - 雙擊
btnStop創建單擊事件處理程式
修改/添加代碼到 Form1.cs
- In Solution Explorer, right-click Form1.cs
- Select View Code
Copy desired .mp4 file (ex: ocean.mp4) to the folder that your .exe file exists in.
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MediaPlayerTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.uiMode = "none";
}
private void btnPlay_Click(object sender, EventArgs e)
{
string filename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().FullName), "ocean.mp4");
axWindowsMediaPlayer1.URL = filename;
}
private void btnStop_Click(object sender, EventArgs e)
{
//stop
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
}
}
Test Media Player:
- Run your program
- Click Play button
Resources:
- Embedding the Windows Media Player Control in a C# Solution
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/311247.html
下一篇:回應式表單重置和表單驗證
