none
MediaElement播放视频始终报找不到文件,怎么回事 RRS feed

  • 问题

  • <Window x:Class="videoPlayTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:videoPlayTest"
            mc:Ignorable="d"
            Title="MainWindow" Height="500" Width="525">
        <Window.Resources>
            <local:ProgressConverter x:Key="PrgConverter"></local:ProgressConverter>
            <Style x:Key="btnStyle" TargetType="Button">
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Offset="0" Color="White"/>
                        <GradientStop Offset="0.5" Color="#FF554D4A"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="FontStyle" Value="Italic"/>
            <Setter Property="Margin" Value="5"/>
            <Setter Property="Width" Value="60"/>
            <Setter Property="Foreground" Value="Gold"/>
            <Style.Triggers>
                <Trigger Property="Button.IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Black"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        </Window.Resources>
        <Grid>
            <StackPanel HorizontalAlignment="Center" Margin="20">
                <Border BorderThickness="3" Background="Black">
                    <Border.BorderBrush>
                        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                            <GradientStop Offset="0" Color="White"/>
                            <GradientStop Offset="0.5" Color="Gold"/>
                        </LinearGradientBrush>
                    </Border.BorderBrush>
                    <MediaElement Height="300" Width="450" Name="mediaElement" 
                          Volume="0.5" LoadedBehavior="Manual" MediaFailed="mediaElement_MediaFailed"
                          MouseLeftButtonUp="mediaElement_MouseLeftButtonUp"/>
                </Border>
                <StackPanel Orientation="Horizontal" Height="40" HorizontalAlignment="Center">
                    <Button x:Name="openBtn" Content="Open File" 
                    Style="{StaticResource btnStyle}" Click="openBtn_Click"/>
                    <Button x:Name="playBtn" Content="Play" 
                    Style="{StaticResource btnStyle}" Click="playBtn_Click"/>
                    <Button x:Name="stopBtn" Content="Stop" 
                    Style="{StaticResource btnStyle}" Click="stopBtn_Click"/>
                    <Button x:Name="backBtn" Content="Back" 
                    Style="{StaticResource btnStyle}" Click="backBtn_Click"/>
                    <Button x:Name="forwardBtn" Content="Forward" 
                    Style="{StaticResource btnStyle}" Click="forwardBtn_Click"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                    <TextBlock Text="Volume" Foreground="Gold"/>
                    <Slider x:Name="volumeSlider" Minimum="0" Maximum="1" Value="0.5" Width="200"/>
                    <Slider x:Name="sldProgress" Minimum="0" Value="{Binding ElementName=mediaElement, Path=Position, Mode=TwoWay, Converter={StaticResource PrgConverter}}" LargeChange="10" Width="192" />
                </StackPanel>
            </StackPanel>
        </Grid>
    </Window>

    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 Microsoft.Win32;
    
    namespace videoPlayTest
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        /// 
    
    
        public class ProgressConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return ((TimeSpan)value).TotalSeconds;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return TimeSpan.FromSeconds((double)value);
            }
        }
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                mediaElement.MediaOpened += MediaElement_MediaOpened;
                
            }
    
            private void MediaElement_Loaded(object sender, RoutedEventArgs e)
            {
               
            }
    
            private void MediaElement_MediaOpened(object sender, RoutedEventArgs e)
            {
                sldProgress.Maximum = mediaElement.NaturalDuration.TimeSpan.TotalSeconds;
            }
    
            private void mediaElement_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                PlayerPause();
            }
    
            private void openBtn_Click(object sender, RoutedEventArgs e)
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = "视频文件|*.*";
                if (dialog.ShowDialog() == true)
                {
                    mediaElement.Source = new Uri(dialog.FileName, UriKind.RelativeOrAbsolute);
                   
                    playBtn.IsEnabled = true;
                }
            }
    
            private void playBtn_Click(object sender, RoutedEventArgs e)
            {
                PlayerPause();
            }
    
            private void stopBtn_Click(object sender, RoutedEventArgs e)
            {
    
            }
    
            private void backBtn_Click(object sender, RoutedEventArgs e)
            {
                mediaElement.Position = mediaElement.Position - TimeSpan.FromSeconds(10);
            }
    
            private void forwardBtn_Click(object sender, RoutedEventArgs e)
            {
                mediaElement.Position = mediaElement.Position + TimeSpan.FromSeconds(10);
            }
    
            private void SetPlayer(bool bPlay)
            {
                backBtn.IsEnabled = bPlay;
                forwardBtn.IsEnabled = bPlay;
                openBtn.IsEnabled = bPlay;
                stopBtn.IsEnabled = bPlay;
                playBtn.IsEnabled = bPlay;
            }
            private void PlayerPause()
            {
                SetPlayer(true);
                if (playBtn.Content.ToString() == "Play")
                {
                    mediaElement.Play();
                    playBtn.Content = "Pause";
                    mediaElement.ToolTip = "Click to Pause";
                }
                else
                {
                    mediaElement.Pause();
                    playBtn.Content = "Play";
                    mediaElement.ToolTip = "Click to Play";
                }
            }
    
            private void mediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
            {
                int i = 0;
            }
        }
    }
    对话框打开文件取绝对路径是没有问题的啊,但感觉视频从来没加载成功过,设置source属性后hasAudio和hasVideo一直是false, failed里面报的是System.Exception {System.IO.FileNotFoundException},怎么回事

    2016年7月13日 1:56

答案

全部回复