![](/img/trans.png)
[英]In Silverlight, how do you find out whether the code is running in the UI thread or not?
[英]How do you test whether Silverlight can play a stream?
我想寫一個像下面這樣的方法。 但是,如果Silverlight無法本地播放該流,我希望它遍歷MediaStreamSource類的列表,然后依次嘗試每個類,直到它們中的任何一個起作用或沒有更多嘗試為止。 我的問題是,如何確定以下方法對於特定流是否足夠。
public static void OpenMedia(this MediaElement ME, Stream FileData)
{
ME.SetSource(FileData);
}
如果無法播放媒體,我需要執行一些代碼。
我不確定您是要尋找測試失敗還是要測試播放媒體類型的能力...
您可以在XAML中添加MediaFailed的事件處理程序,也可以在兩種情況下在其后面編寫代碼。 主動檢查可玩性將需要一些創造力,例如擁有一些很小(<1秒)且無聲的各種“樣本”媒體文件,這些文件用於測試可玩性,並且對用戶是透明的。
對於在事件后面添加代碼的C#,並添加新事件:
void yourPage_Loaded(object sender, RoutedEventArgs e)
{
ME.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(ME_MediaFailed);
}
void ME_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
add your code to handle the exception here.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.