How can I play video files? [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
开发者_JS百科 Improve this questionI like to play video files, such as AVIs, through my C# program. Is it possible to play video files like that?
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:
// [ 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
You might consider using the Audio/Video controls in Managed DirectX as a quick solution:
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).
One other plus of using DirectShow is that windows will handle loading the necessary codecs and rendering components necessary for a given media type.
You could always use Silverlight to show video content and then plug the silverlight application into your web page.
What follows are some articles on creating a video player in silverlight:
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
精彩评论