不使用媒体播放器播放视频 [Winform]

Posted

技术标签:

【中文标题】不使用媒体播放器播放视频 [Winform]【英文标题】:Play video without using media player [Winform] 【发布时间】:2013-07-04 04:01:49 【问题描述】:

我想像那个人 [link] 那样播放视频。

我正在开发 C# Windows 窗体应用程序(不是 NXA)。 但我不知道怎么做。 我尝试使用Microsoft.DirectX.AudioVideoPlayback,但没有成功。

这是我迄今为止尝试过的:

OpenFileDialog rihanna = new OpenFileDialog();
if(rihanna.ShowDialog() == DialogResult.OK)

    video = new Video(rihanna.FileName);
    video.Owner = panel1;    
    video.Stop();    

现在我该怎么办?我尝试使用视频课程,但正如我所说,它不起作用。 我可以编译,但是当我运行程序时,我看不到表单窗口。

【问题讨论】:

【参考方案1】:
using Microsoft.DirectX.AudioVideoPlayback;

namespace Play_Video


public partial class Form1 : Form

    Video vdo;

    public string mode="play";
    public string PlayingPosition, Duration;
    public Form1()
    
        InitializeComponent();
        VolumeTrackBar.Value = 4;
    



    private void timer1_Tick(object sender, EventArgs e)
    

        PlayingPosition = CalculateTime(vdo.CurrentPosition);
        txtStatus.Text = PlayingPosition + "/" + Duration;

        if (vdo.CurrentPosition >= vdo.Duration)
        
            timer1.Stop();
            Duration = CalculateTime(vdo.Duration);
            PlayingPosition = "0:00:00";
            txtStatus.Text = PlayingPosition + "/" + Duration;
            vdo.Stop();
            btnPlay.BackgroundImage = Play_Video.Properties.Resources.btnplay;
            vdoTrackBar.Value = 0;
        
        else
            vdoTrackBar.Value += 1;

    

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    
        if (vdo != null)
        
            vdo.Stop();
            timer1.Stop();
            btnPlay.BackgroundImage = Play_Video.Properties.Resources.btnplay;
            vdoTrackBar.Value = 0;

        
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.ShowDialog();
        openFileDialog1.Title = "Select video file..";
        openFileDialog1.InitialDirectory = Application.StartupPath;
        openFileDialog1.DefaultExt = ".avi";
        openFileDialog.Filter = "Media Files|*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3|All Files|*.*";
        if (openFileDialog1.FileName != "")
        
            Form1.ActiveForm.Text = openFileDialog.FileName + " - Anand Media Player";
            vdo = new Video(openFileDialog.FileName);

            vdo.Owner = panel1;
            panel1.Width = 700;
            panel1.Height = 390;
            Duration = CalculateTime(vdo.Duration);
            PlayingPosition = "0:00:00";
            txtStatus.Text = PlayingPosition + "/" + Duration;

            vdoTrackBar.Minimum = 0;
            vdoTrackBar.Maximum = Convert.ToInt32(vdo.Duration);
        
    

    private void btnPlay_Click(object sender, EventArgs e)
    

        if (vdo != null)
        
            if (vdo.Playing)
            
                vdo.Pause();
                timer1.Stop();
                btnPlay.BackgroundImage = Play_Video.Properties.Resources.btnplay;
            
            else
            
                vdo.Play();
                timer1.Start();

                btnPlay.BackgroundImage = Play_Video.Properties.Resources.pause;
            
        

    

    private void btnStop_Click(object sender, EventArgs e)
    
        vdo.Stop();
        timer1.Stop();
        btnPlay.BackgroundImage = Play_Video.Properties.Resources.btnplay;
        vdoTrackBar.Value = 0;
    

    public string CalculateTime(double Time)
    
        string mm, ss, CalculatedTime;
        int h, m, s, T;

        Time = Math.Round(Time);
        T = Convert.ToInt32(Time);

        h = (T / 3600);
        T = T % 3600;
        m = (T / 60);
        s = T % 60;

        if (m < 10)
            mm = string.Format("00", m);
        else
            mm = m.ToString();
        if (s < 10)
            ss = string.Format("00", s);
        else
            ss = s.ToString();

        CalculatedTime = string.Format("0:1:2", h, mm, ss);

        return CalculatedTime;
    

    private void VolumeTrackBar_Scroll(object sender, EventArgs e)
    
        if (vdo != null)
        
            vdo.Audio.Volume = CalculateVolume();
        
    
    public int CalculateVolume()
    
      switch (VolumeTrackBar.Value)
        
            case 1:
                return -1500;
            case 2:
                return -1000;
            case 3:
                return -700;
            case 4:
                return -600;
            case 5:
                return -500;
            case 6:
                return -400;
            case 7:
                return -300;
            case 8:
                return -200;
            case 9:
                return -100;
            case 10:
                return 0;
            default:
                return -10000;
        
    

    private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
    
        Duration = CalculateTime(vdo.Duration);
        PlayingPosition = "0:00:00";
        txtStatus.Text = PlayingPosition + "/" + Duration;
    

    private void vdoTrackBar_Scroll(object sender, EventArgs e)
    
        if (vdo != null)
        
            vdo.CurrentPosition = vdoTrackBar.Value;
        
    

    private void Form1_Load(object sender, EventArgs e)
    
        MaximizeBox = false;
    

    private void exitToolItem_Click(object sender,EventArgs e)
    
        Application.Exit();
    


【讨论】:

从哪里获得Microsoft.DirectX Microsoft.DirectX 是 XNA 的一个替代品。如果我没记错的话,最后一个支持它的 Visual Studio 是 2010 年【参考方案2】:

Okey 命名空间清晰:

using Microsoft.DirectX.AudioVideoPlayback;

表单中的一些全局变量:

Video vdo;
public string mode="play";
public string PlayingPosition, Duration;

现在在您的 Button 或其他要打开的内容中:

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
openFileDialog1.Title = "Select video file..";
openFileDialog1.InitialDirectory = Application.StartupPath;
openFileDialog1.DefaultExt = ".avi";
openFileDialog.Filter = "Media Files|*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3|All Files|*.*";
vdo = new Video(openFileDialog.FileName);

vdo.Owner = panel1;
panel1.Width = 700;
panel1.Height = 390;
Duration = CalculateTime(vdo.Duration);
PlayingPosition = "0:00:00";
txtStatus.Text = PlayingPosition + "/" + Duration;

vdoTrackBar.Minimum = 0;
vdoTrackBar.Maximum = Convert.ToInt32(vdo.Duration);

在其他一些按钮代码中开始/暂停:

if (vdo.Playing)

   vdo.Pause();
   btnPlay.Text= "Play";

else

   vdo.Play();
   btnPlay.Text= "Pause";


顺便说一句: 不要在代码中在女孩之后命名变量/成员或其他东西...

如果你不确定如何命名,这里有一些Guidelines。

目标是提供一组一致的命名 导致名称立即有意义 开发人员。

【讨论】:

她没有在女孩之后使用变量名称,她正在播放视频,并且可能有一个带有视频路径的常量文件,该常量被命名为“rihanna.FileName”所以我猜这是在说这是蕾哈娜的视频(如果你不知道,蕾哈娜是一名歌手,所以这可能是视频剪辑之类的。) @GabrielVonlantenC.Lopes 这将是 string 的名称,而不是 OpenFileDialog 实例的名称 当我尝试运行时出现异常:无法加载文件或程序集 'Microsoft.DirectX.AudioVideoPlayback, Version=1.0.2902.0【参考方案3】:

要使 AudioVideoPlayback 正常工作,您需要通过 Reference > Add Reference > Browse > C: > Windows > Microsoft.Net > DirectX for managed code > 1.0.2902.0 > Microsoft.DirectX.AudioVideoPlayback 添加 AudioVideoPlayback 引用。 dll

【讨论】:

当我尝试运行时出现异常:无法加载文件或程序集 'Microsoft.DirectX.AudioVideoPlayback, Version=1.0.2902.0

以上是关于不使用媒体播放器播放视频 [Winform]的主要内容,如果未能解决你的问题,请参考以下文章

H265流媒体播放器EasyPlayer如何设置视频关闭自动播放?

如何保护通过 Media Source Extension API 播放的视频文件?

HTTP协议下可拖动时间轴播放FLV的实现(伪流媒体)

DMP是啥意思

[Linux]流媒体服务器概述

Android 音频播放——AudioTrack直接播PCMMediaPlayer播媒体文件可以是audio