我尝试在 WPF 上制作媒体播放器。
我做了这个:
public partial class MyMediaPlayer : Window
{
public MyMediaPlayer()
{
InitializeComponent();
//
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = "c:\\"; // init
dlg.Filter = "All Files (*.*)|*.*"; // filter
dlg.RestoreDirectory = true;
// dialog window
if (dlg.ShowDialog() == true) // checked ?
{
string selectedFileName = dlg.FileName; // path of the media
MediaPlayer player = new MediaPlayer();
player.Open(new Uri(selectedFileName, UriKind.Relative));
VideoDrawing aVideoDrawing = new VideoDrawing();
aVideoDrawing.Rect = new Rect(0, 0, 100, 100);
aVideoDrawing.Player = player; // play
// never play
player.Play();
}
}
}
和 XAML 文件:
<Window ... >
<Grid>
<MediaElement Margin="10,10,10,0 " Source="D:\test.avi"
Name="McMediaElement"
Width="450" Height="250" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill"
/>
</Grid>
</Window>
但是,视频永远不会开始,并且窗口保持白色。
请帮忙 :)
ps:对不起我的英语不好