2015-03-31 100 views
0

我想顯示視頻。我希望視頻可以通過點擊按鈕來播放。 視頻無法播放。 我把視頻放到了項目中。WPF:爲什麼MediaElement不玩?

我希望視頻源是YouTube。

我的XAML代碼:

<Window x:Class="MediaElementApp.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="467.91" Width="1300"> 
<Grid> 
    <MediaElement x:Name="mediaElement" HorizontalAlignment="Left" Height="418" Margin="246,10,0,0" VerticalAlignment="Top" Width="1036" LoadedBehavior="Manual" UnloadedBehavior="Stop" Source="Images\Wildlife.wmv" /> 
    <Button x:Name="play" HorizontalAlignment="Left" Margin="538,161,0,0" VerticalAlignment="Top" Width="100" Height="84" Click="play_Click" > 
     <Button.Background> 
      <ImageBrush ImageSource="Images/smiley.jpg"/> 
     </Button.Background> 
    </Button> 

</Grid> 

C#代碼:

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; 

namespace MediaElementApp 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

     private void play_Click(object sender, RoutedEventArgs e) 
     { 

      mediaElement.Play(); 
     } 
    } 
} 

我想尋求幫助。

回答

0

媒體來源應該是從你的文件系統例如

mediaElement.Source = new Uri(@"C:\User\Admin\Images\Wildlife.wmv") 
+0

謝謝。你知道如果我想從YouTube獲得視頻,該怎麼辦? – 2015-03-31 19:45:35

0

是,喬B是正確的。另一種方法是使用後面所示方法的代碼,但這需要將媒體文件放置在應用程序內的子文件夾中。

var videoPath = Directory.GetCurrentDirectory(); 
mediaElement.Source = new Uri(videoPath + @"\Images\Wildlife.wmv", UriKind.Relative); 
mediaElement.Play();