2016-04-22 63 views
0

当用户点击'播放'按钮时,我的程序应该播放视频。然而,“Play”的第一次点击却什么都不做。'MediaElement.CurrentState'由于未知原因而变为'Closed'

该代码,这是非常简单的,它只是调用我的MediaElement,录像机“的SetSource”,然后播放:

private async void playVideo_Tapped(object sender, TappedRoutedEventArgs e) 
{ 
    await setUpVideo(); 
    VideoPlayer.Play(); 
} 

我经历过“setUpVideo()”多次,那里的一切像它应该那样工作,在确定文件存在后它只调用'VideoPlayer.SetSource()'。但直到我在一个方法抛出监控“VideoPlayer.CurrentState”,我才意识到了问题的状态:

public VideoViewer() 
{ 
    this.InitializeComponent(); 
    VideoPlayer.CurrentStateChanged += VideoPlayer_CurrentStateChanged; 
} 

void VideoPlayer_CurrentStateChanged(object sender, RoutedEventArgs e) 
{ 
    var foo = VideoPlayer.CurrentState; 
} 

如果我检查“富”,而我的代码运行我看到的价值在'playVideo_Tapped()'完成后,第一次轻敲(并且只有第一次轻敲)'VideoPlayer.CurrentState'变为'Opening',然后变回'Closed'!之后的每一次敲击都会按照'打开'到'播放'然后'已暂停'的正确顺序进行,但第一次敲击总是会关闭。为什么是这样??

回答

1

看起来问题毕竟在'setUpVideo()'中。 Woops。

短版,这个问题是由从这种变化中的一段代码 'setUpVideo()' 固定:

using (IRandomAccessStream fileStream = await videoFile.OpenAsync(FileAccessMode.Read)) 
{ 
    VideoPlayer.SetSource(fileStream, videoFile.ContentType); 
} 

...这样的:

IRandomAccessStream fileStream = await videoFile.OpenAsync(FileAccessMode.Read); 
VideoPlayer.SetSource(fileStream, videoFile.ContentType); 

加长版,我代码失败,因为错误“mf_media_engine_err_src_not_supported hresult - 0xc00d36c4”,它正在关闭我的MediaElement而不是播放它。发生这种情况是因为当我离开'使用'代码块时,'IRandomAccessStream'会在我读取文件的过程中关闭。我并不是100%清楚为什么它在代码的第一次运行后才能完成整个事情,但至少现在它可以可靠地工作。

我也必须给信贷在哪里信用到期,我在这里找到了这个答案:Windows 8 app - MediaElement not playing ".wmv" files

相关问题