1

我想用 C# 播放视频。我搜索了相关教程,我发现了这个。我遵循完全相同,但视频不会出现。

这是我的代码。

   using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.AudioVideoPlayback;

namespace WindowsFormsApplication4
{    
public partial class Form1 : Form
{
    Video video;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            // store the original size of the panel
            int width = panel1.Width;
            int height = panel1.Height;

            // load the selected video file
            //video = new Video("C:\\Users\\HDAdmin\\Desktop\\Example.avi");
            video = new Video(openFileDialog1.FileName);

            // set the panel as the video object’s owner
            video.Owner = panel1;

            // stop the video
            video.Stop();

            // resize the video to the size original size of the panel
            panel1.Size = new Size(width, height);

            try
            {
                video.Audio.Volume = 100;
            }
            catch { }
        }

    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (video.State != StateFlags.Running)
        {
            video.Play();
        }

    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (video.State == StateFlags.Running)
        {
            video.Pause();
        }


    }

    private void button4_Click(object sender, EventArgs e)
    {
        if (video.State != StateFlags.Stopped)
        {
            video.Stop();
        }


    }
    }
}

我什至试图像这样放置视频的路径:

video = new Video("C:\\Users\\HDAdmin\\Desktop\\Example.avi");

但两者都会显示如下图所示的错误。 在此处输入图像描述

我的问题是,我想如何通过在 c# 中使用 windows 窗体应用程序来查看和播放视频?

p/s:我只想使用 Directx,不想在 windows 媒体播放器中播放视频。

4

0 回答 0