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

标签 c# winforms video audiovideoplayback

我想像那个人那样播放视频 [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();    
}

现在我能做什么?我尝试使用视频类(class),但正如我所说,它根本行不通。 我可以编译,但是当我运行程序时,我看不到窗体窗口。

最佳答案

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("0{0}", m);
        else
            mm = m.ToString();
        if (s < 10)
            ss = string.Format("0{0}", 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();
    }
}
}

关于c# - 不使用媒体播放器播放视频[Winform],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17461670/

相关文章:

c# - 如何避免在 aspx 页面中重复命名空间

c# - System.Controls.ComboBox - 如何让它添加 Itemssource 中不存在的项目?

c# - 直接将 C# 代码添加到 exe 文件,而不使用 Visual Studio 中的构建命令

C# 在 datagridview 中搜索重复项

c# - ListView 文本被截断——如何显示项目的完整文本?

c# - C#程序使用秒表卡住以对 Action 计时

javascript - Kinetic Js Canvas 视频与 JavaScript

javascript - 如何在javascript中从youtube获取视频数据

c# - 结构图 Automocker 注入(inject)如何工作?

video - 使用 AVFoundation 录制的视频可以直接写入 Assets 库吗?