C#中的媒体播放器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18846249/
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
Media player in C#
提问by
Well I am making a media player in the C# using the System.Media.SoundPlayer
class but there are too many limitations in that like it only plays .wav
files and there is no volume control in this and also there is no media-progessbar
. So is there any better builtin class or custom class. if there is any please specify. Or even if there is any thing which will work parallel with it.
好吧,我正在使用System.Media.SoundPlayer
该类在 C# 中制作媒体播放器,但是有太多限制,例如它只能播放.wav
文件,并且没有音量控制,也没有media-progessbar
. 那么有没有更好的内置类或自定义类。如果有请说明。或者即使有任何东西可以与之并行工作。
采纳答案by
You can use the WPF MediaElementas full fledged audio and video player.
您可以将 WPF MediaElement用作成熟的音频和视频播放器。
It is very easy to handle and brings more or less all functionality Windows Media Player has. If you use Windows Forms you can embedd the WPF control easily.
它非常易于处理,并或多或少地提供了 Windows Media Player 的所有功能。如果您使用 Windows 窗体,则可以轻松嵌入 WPF 控件。
The only drawback in my optinion is that it needs Windows Media Player to be installed.
我选择的唯一缺点是它需要安装 Windows Media Player。
回答by keyboardP
You can use the Windows Media Player Control. An example is shown on MSDN.
您可以使用Windows Media Player 控件。MSDN上显示了一个示例。
private void PlayFile(String url)
{
Player = new WMPLib.WindowsMediaPlayer();
Player.URL = url;
Player.controls.play();
}
If you want more control over playback, and don't want to tie yourself to WMP, you could look into using NAudiowhich is an open-source media library.
如果您想要更多地控制播放,并且不想将自己绑定到 WMP,您可以考虑使用NAudio,这是一个开源媒体库。
回答by thilipan
hey there are so many examples. And you did not supply enough information. i think these might help you to start
嘿有这么多的例子。而且你没有提供足够的信息。我认为这些可能会帮助你开始
http://www.codeproject.com/Articles/2632/DirectShow-MediaPlayer-in-C
http://www.codeproject.com/Articles/2632/DirectShow-MediaPlayer-in-C
回答by Abhishek Dey
You need not worry about the file extensions if you use the MediaElement available in the toolbox.
如果您使用工具箱中提供的 MediaElement,则无需担心文件扩展名。
Here's a snippet that can help you:
这是一个可以帮助您的片段:
private void button4_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.AddExtension = true;
ofd.DefaultExt = "*.*";
ofd.Filter = "Media(*.*)|*.*";
ofd.ShowDialog();
mediaElement1.MediaOpened += new RoutedEventHandler(mediaElement1_MediaOpened);
mediaElement1.Source = new Uri(ofd.FileName);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
mediaElement1.Play();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
mediaElement1.Stop();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
mediaElement1.Pause();
}
For more details refer to: http://bitsandbinaries.wordpress.com/net-programming/wpfwindows-presentation-foundation/a-simple-media-player-program-in-c-net-4-0/
更多详情请参考:http: //bitsandbinaries.wordpress.com/net-programming/wpfwindows-presentation-foundation/a-simple-media-player-program-in-c-net-4-0/
回答by Ian Mercer
If you want more control, use NAudio (http://naudio.codeplex.com/) or SharpDX (http://sharpdx.org/). Using SharpDX I have built a media player that drives multiple sound cards (for different zones) with smooth fade-out for terminated song playback, ducking for announcements, etc..
如果您想要更多控制,请使用 NAudio ( http://naudio.codeplex.com/) 或 SharpDX ( http://sharpdx.org/)。使用 SharpDX 我构建了一个媒体播放器,它可以驱动多个声卡(用于不同区域),具有平滑的淡出以终止歌曲播放,躲避通知等。