2016-04-22 67 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

相關問題