C# 如何播放视频文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2127445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:42:40  来源:igfitidea点击:

How can I play video files?

c#videofile

提问by ratty

I like to play video files, such as AVIs, through my C# program. Is it possible to play video files like that?

我喜欢通过我的 C# 程序播放视频文件,例如 AVI。可以播放这样的视频文件吗?

采纳答案by Brian Hasden

You should be able to use the Media Player control to play media files.

您应该能够使用媒体播放器控件播放媒体文件。

Example of playing audio from http://msdn.microsoft.com/en-us/library/dd562692(VS.85).aspx, you should be able to adapt it to video:

http://msdn.microsoft.com/en-us/library/dd562692(VS.85).aspx播放音频的示例,您应该能够将其调整为视频:

// [ C# ]
WMPLib.WindowsMediaPlayer Player;

private void PlayFile(String url)
{
    Player = new WMPLib.WindowsMediaPlayer();
    Player.PlayStateChange += 
        new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
    Player.MediaError += 
        new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);
    Player.URL = url;
    Player.controls.play();
}

private void Form1_Load(object sender, System.EventArgs e)
{
    // TODO  Insert a valid path in the line below.
    PlayFile(@"c:\myaudio.wma");
}

private void Player_PlayStateChange(int NewState)
{
    if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
    {
        this.Close();
    }
}

private void Player_MediaError(object pMediaObject)
{
    MessageBox.Show("Cannot play media file.");
    this.Close();
}

There's a bit more information available on MSDN at http://msdn.microsoft.com/en-us/library/dd564582(VS.85).aspx

MSDN 上有更多信息,网址http://msdn.microsoft.com/en-us/library/dd564582(VS.85).aspx

回答by Alistair Evans

You might consider using the Audio/Video controls in Managed DirectX as a quick solution:

您可以考虑使用 Managed DirectX 中的音频/视频控件作为快速解决方案:

http://msdn.microsoft.com/en-us/library/bb324497%28VS.85%29.aspx#dx_avp_playing_a_video_file

http://msdn.microsoft.com/en-us/library/bb324497%28VS.85%29.aspx#dx_avp_playing_a_video_file

If you need more control over the video, or better integration with your application, you can use DirectShow. There is a good C# interop library for accessing it (DirectShowLib).

如果您需要更多地控制视频,或者更好地与您的应用程序集成,您可以使用 DirectShow。有一个很好的 C# 互操作库来访问它(DirectShowLib)。

One other plus of using DirectShow is that windows will handle loading the necessary codecs and rendering components necessary for a given media type.

使用 DirectShow 的另一个好处是窗口将处理加载必要的编解码器和给定媒体类型所需的渲染组件。

回答by Aim Kai

You could always use Silverlightto show video content and then plug the silverlight application into your web page.

您始终可以使用Silverlight来显示视频内容,然后将 Silverlight 应用程序插入到您的网页中。

What follows are some articles on creating a video player in silverlight:

以下是一些关于在 Silverlight 中创建视频播放器的文章:

http://www.85turns.com/2008/04/02/create-a-video-player-silverlight-2-part-1/

http://www.85turns.com/2008/04/02/create-a-video-player-silverlight-2-part-1/

or

或者

http://weblogs.asp.net/dwahlin/archive/2008/03/07/silverlight-2-0-video-tutorials.aspx

http://weblogs.asp.net/dwahlin/archive/2008/03/07/silverlight-2-0-video-tutorials.aspx