使用C#在WPF中使用Media Player [英] Media Player in WPF using C#
问题描述
我正在WPF中创建一个应用程序,并希望有一个播放音频和视频文件的媒体播放器.我已经创建了一个.但是问题是它可以正确编译并执行,但不能给我音频或视频输出.
我还向您发送了XAML和C#编码.如果可以的话请帮助我.
提前谢谢...
XAML编码:
I am creating a an application in WPF and want to have a media player that plays audio and video files. I have created one. But the problem is that it compiles and executes properly but does not give me audio or video output.
I am also sending you the XAML and C# coding of it. Please help me if possible.
Thanks in advance...
XAML Coding:
<pre lang="xml"><Window x:Class="MediaPlayer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="226*" />
<RowDefinition Height="36*" />
</Grid.RowDefinitions>
<MediaElement x:Name="MediaEL" Grid.RowSpan="1" LoadedBehavior="Manual" UnloadedBehavior="Close" />
<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
<Button x:Name="btnPlay" Content="Play" Click="btnPlay_Click"
Width="50" Height="25"/>
<Button x:Name="btnStop" Content="Stop" Click="btnStop_Click"
Width="50" Height="25"/>
<Button x:Name="btnMoveBackward" Content="Back" Click="btnMoveBackward_Click"
Width="50" Height="25"/>
<Button x:Name="btnMoveForward" Content="Forward" Click="btnMoveForward_Click"
Width="50" Height="25"/>
<Button x:Name="btnOpen" Content="Open" Click="btnOpen_Click"
Width="50" Height="25"/>
</StackPanel>
</Grid>
</Window
>
C#代码:
>
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
namespace MediaPlayer
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class MainWindow : Window
{
#region Constructor
public MainWindow()
{
InitializeComponent();
IsPlaying(false);
}
#endregion
//******************************************
#region IsPlaying(bool)
private void IsPlaying(bool bValue)
{
btnStop.IsEnabled = bValue;
btnMoveBackward.IsEnabled = bValue;
btnMoveForward.IsEnabled = bValue;
btnPlay.IsEnabled = bValue;
}
#endregion
//***************************************
#region Open Media
private void btnOpen_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Audio and Video (*.mp3,*.wav,*.mpeg,*.wmv,*.avi)|*.mp3;*.wav;*.mpeg;*.wmv;*.avi"; // Filter files by extension
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MediaEL.Source = new Uri(ofd.FileName);
btnPlay.IsEnabled = true;
}
}
#endregion
//***************************************
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
IsPlaying(true);
if (btnPlay.Content.ToString() == "Play")
{
MediaEL.Play();
btnPlay.Content = "Pause";
}
else
{
MediaEL.Pause();
btnPlay.Content = "Play";
}
}
//***************************************
#region Stop
private void btnStop_Click(object sender, RoutedEventArgs e)
{
MediaEL.Stop();
btnPlay.Content = "Play";
IsPlaying(false);
btnPlay.IsEnabled = true;
}
#endregion
//***************************************
private void btnMoveBackward_Click(object sender, RoutedEventArgs e)
{
MediaEL.Position = MediaEL.Position + TimeSpan.FromSeconds(10);
}
private void btnMoveForward_Click(object sender, RoutedEventArgs e)
{
MediaEL.Position = MediaEL.Position - TimeSpan.FromSeconds(10);
}
}
}
此外,在阅读了某些博客之后,我了解到必须将media元素的加载的行为属性设置为手动.我也将这些更改纳入其中.但这仍然没有给我收获任何成果.只需遍历这段代码即可指导我解决这个问题.
Further, after reading certain blogs, I have learnt that one has to set the loaded behavior property of the media element as manual. I have also incorporated these changes in it. But still it did not reap me any fruits. Just go through this code and guide me in this concern.
推荐答案
这里有一个博客,解释了如何在WPF中创建媒体播放器:
http://www.studentguru.gr/blogs/dante/archive/2011/01/03/121754.aspx [ ^ ]
请检查第二部分.
在快速浏览了您提供的代码后,
也添加这些事件:
1)MediaOpened:将isPlaying设置为true
2)MediaEnded:通过提供MediaEL.stop()来停止MediaElement 3)将LoadBehavior设置为手动"
4)UnloadedBehavior停止
该博客具有完整的解决方案,也请查看其第二部分: http://www.studentguru.gr/blogs/dante/archive/2011/01/12/122329.aspx [
There is a blog here which explained how to create a Media Player in WPF :
http://www.studentguru.gr/blogs/dante/archive/2011/01/03/121754.aspx[^]
Do check out its second part.
Also after having a quick review of the code that you provided,
Add these events too :
1) MediaOpened : to set the isPlaying to true
2) MediaEnded : to stop the MediaElement by providing MediaEL.stop()
3) Set the LoadBehavior to Manual
4) UnloadedBehavior to Stop
The blog has the complete solution, check out its second part too : http://www.studentguru.gr/blogs/dante/archive/2011/01/12/122329.aspx[^]
All the best.
这篇关于使用C#在WPF中使用Media Player的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!