Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#MediaElement——为什么Play()有时在切换源代码后会自动失败?_C#_Wpf_Video_Mediaelement - Fatal编程技术网

C#MediaElement——为什么Play()有时在切换源代码后会自动失败?

C#MediaElement——为什么Play()有时在切换源代码后会自动失败?,c#,wpf,video,mediaelement,C#,Wpf,Video,Mediaelement,我在自定义用户控件中设置了一个MediaElement,用于制作视频播放器控件--播放/暂停按钮、滑块、剩余时间等。我将scrubingEnabled设置为True,以便我可以根据so post向用户显示视频的第一帧,并使用滑块元素允许用户拖动视频 问题:我使用绑定切换视频播放器的源。有时,如果在播放视频时切换视频,MediaElement会停止响应Play()命令。即使在mediafiled事件中,也不会给出错误。每次调用Play()(或Pause()然后Play())都会失败。我可以在Med

我在自定义用户控件中设置了一个
MediaElement
,用于制作视频播放器控件--播放/暂停按钮、滑块、剩余时间等。我将
scrubingEnabled
设置为
True
,以便我可以根据so post向用户显示视频的第一帧,并使用
滑块
元素允许用户拖动视频

问题:我使用绑定切换视频播放器的源。有时,如果在播放视频时切换视频,
MediaElement
会停止响应
Play()
命令。即使在
mediafiled
事件中,也不会给出错误。每次调用
Play()
(或
Pause()
然后
Play()
)都会失败。我可以在
MediaElement
失败后切换视频源,然后它将重新开始工作

XAML:

如果我让视频一直自动播放,那么播放器100%的时间都在工作,真奇怪。不幸的是,我需要视频暂停时,源设置有人知道如何在交换视频、显示加载视频的第一帧等的同时避免MediaElement的随机隐藏故障吗?


有一些奇怪的相似问题,但我的问题有不同的症状,因为我只使用了1
MediaElement

,奇怪的是,如果将
scrubingEnabled
设置为
False
,那么
MediaElement
不再在随机时间停止响应。禁用
scrubingEnabled
会中断第一帧的显示,并使
滑块
元素无法正常工作,因此这里介绍了如何在没有中断的
MediaElement
的情况下保持这些功能

显示第一帧:

private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    MediaElementVideoPlayer player = d as MediaElementVideoPlayer;
    if (player != null)
    {
        player.Dispatcher.Invoke(() => {
            if (player.VideoSource != null && player.VideoSource != "")
            {
                if (player._isPlaying)
                {
                    player.VideoPlayer.Stop();
                    var uriSource = new Uri(@"/ImageResources/vid-play.png", UriKind.Relative);
                    player.PlayPauseImage.Source = new BitmapImage(uriSource);
                    player._isPlaying = false;
                } 
                player.VideoPlayer.Source = new Uri(player.VideoSource);
                // Start the video playing so that it will show the first frame. 
                // We're going to pause it immediately so that it doesn't keep playing.
                player.VideoPlayer.IsMuted = true; // so sound won't be heard
                player.VideoPlayer.Play();
            }
        });
    }
}

private void VideoPlayer_MediaOpened(object sender, RoutedEventArgs e)
{
    Dispatcher.Invoke(() =>
    {
        // the video thumbnail is now showing!
        VideoPlayer.Pause();
        // reset video to initial position
        VideoPlayer.Position = TimeSpan.FromTicks(0);
        Player.IsMuted = false; // unmute video
        TimeSlider.Minimum = 0;
        // Set the time slider values & time label
        if (VideoPlayer.NaturalDuration != null && VideoPlayer.NaturalDuration != Duration.Automatic)
        {
            TimeSlider.Maximum = VideoPlayer.NaturalDuration.TimeSpan.TotalSeconds;
            TimeSlider.Value = 0;
            double totalSeconds = VideoPlayer.NaturalDuration.TimeSpan.TotalSeconds;
            _durationString = Utilities.numberSecondsToString((int)totalSeconds, true, true);
            TimeLabel.Content = "- / " + _durationString;
        }
    });
}
// ValueChanged for Slider
private void TimeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    Dispatcher.Invoke(() => {
        TimeSpan videoPosition = TimeSpan.FromSeconds(TimeSlider.Value);
        VideoPlayer.Position = videoPosition;
        TimeLabel.Content = Utilities.numberSecondsToString((int)VideoPlayer.Position.TotalSeconds, true, true) + " / " + _durationString;
    });
}

// DragStarted event
private void slider_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
    Dispatcher.Invoke(() => {
        VideoPlayer.ScrubbingEnabled = true;
        VideoPlayer.Pause();
    });
}

// DragCompleted event
private void slider_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
    Dispatcher.Invoke(() => {
        VideoPlayer.ScrubbingEnabled = false;
        TimeSpan videoPosition = TimeSpan.FromSeconds(TimeSlider.Value);
        VideoPlayer.Position = videoPosition;
        if (_isPlaying) // if was playing when drag started, resume playing
            VideoPlayer.Play();
        else
            VideoPlayer.Pause();
    });
}
在拖动滑块时为视频启用拖动:

private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    MediaElementVideoPlayer player = d as MediaElementVideoPlayer;
    if (player != null)
    {
        player.Dispatcher.Invoke(() => {
            if (player.VideoSource != null && player.VideoSource != "")
            {
                if (player._isPlaying)
                {
                    player.VideoPlayer.Stop();
                    var uriSource = new Uri(@"/ImageResources/vid-play.png", UriKind.Relative);
                    player.PlayPauseImage.Source = new BitmapImage(uriSource);
                    player._isPlaying = false;
                } 
                player.VideoPlayer.Source = new Uri(player.VideoSource);
                // Start the video playing so that it will show the first frame. 
                // We're going to pause it immediately so that it doesn't keep playing.
                player.VideoPlayer.IsMuted = true; // so sound won't be heard
                player.VideoPlayer.Play();
            }
        });
    }
}

private void VideoPlayer_MediaOpened(object sender, RoutedEventArgs e)
{
    Dispatcher.Invoke(() =>
    {
        // the video thumbnail is now showing!
        VideoPlayer.Pause();
        // reset video to initial position
        VideoPlayer.Position = TimeSpan.FromTicks(0);
        Player.IsMuted = false; // unmute video
        TimeSlider.Minimum = 0;
        // Set the time slider values & time label
        if (VideoPlayer.NaturalDuration != null && VideoPlayer.NaturalDuration != Duration.Automatic)
        {
            TimeSlider.Maximum = VideoPlayer.NaturalDuration.TimeSpan.TotalSeconds;
            TimeSlider.Value = 0;
            double totalSeconds = VideoPlayer.NaturalDuration.TimeSpan.TotalSeconds;
            _durationString = Utilities.numberSecondsToString((int)totalSeconds, true, true);
            TimeLabel.Content = "- / " + _durationString;
        }
    });
}
// ValueChanged for Slider
private void TimeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    Dispatcher.Invoke(() => {
        TimeSpan videoPosition = TimeSpan.FromSeconds(TimeSlider.Value);
        VideoPlayer.Position = videoPosition;
        TimeLabel.Content = Utilities.numberSecondsToString((int)VideoPlayer.Position.TotalSeconds, true, true) + " / " + _durationString;
    });
}

// DragStarted event
private void slider_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
    Dispatcher.Invoke(() => {
        VideoPlayer.ScrubbingEnabled = true;
        VideoPlayer.Pause();
    });
}

// DragCompleted event
private void slider_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
    Dispatcher.Invoke(() => {
        VideoPlayer.ScrubbingEnabled = false;
        TimeSpan videoPosition = TimeSpan.FromSeconds(TimeSlider.Value);
        VideoPlayer.Position = videoPosition;
        if (_isPlaying) // if was playing when drag started, resume playing
            VideoPlayer.Play();
        else
            VideoPlayer.Pause();
    });
}
//滑块的值已更改
私有void TimeSlider\u值已更改(对象发送方,RoutedPropertyChangedEventArgs e)
{
Dispatcher.Invoke(()=>{
TimeSpan videoPosition=TimeSpan.FromSeconds(TimeSlider.Value);
视频播放器。位置=视频位置;
TimeLabel.Content=Utilities.numberSecondsToString((int)VideoPlayer.Position.TotalSeconds,true,true)+“/”+_durationString;
});
}
//拖动启动事件
私有无效滑块\u DragStarted(对象发送器,System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
Dispatcher.Invoke(()=>{
VideoPlayer.scrubingEnabled=true;
暂停();
});
}
//拖拉完成事件
private void slider_DragCompleted(对象发送器,System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
Dispatcher.Invoke(()=>{
VideoPlayer.scrubingEnabled=false;
TimeSpan videoPosition=TimeSpan.FromSeconds(TimeSlider.Value);
视频播放器。位置=视频位置;
如果(_isplay)//如果开始拖动时正在播放,请继续播放
视频播放器;
其他的
暂停();
});
}