我正在通过silverlight MediaElement对象播放视频的应用程序。大方法导致silverlight应用程序进入“无响应”状态
我有一个大的方法,它是负责以下
- 视频的本地文件路径上打开一个FileInfo项目和钢带的文件名来获得我们为使用文件名的第一部分许可获取处理
- 的一部分设置在MediaElement的
- 的LicenseAcquirer设置的的MediaElement
Source属性当此方法是称为,它实际上会导致应用程序进入“不能响应”状态几秒钟。我如何避免这种情况?我试图把这一切都放到后台工作,但我不得不调用几乎所有的调用UI线程,这并没有帮助它似乎让事情变得更慢。
我有一个繁忙的框,显示这一切发生,但实际上停止报告进度的应用程序没有响应的那些秒。我明白为什么会发生这种情况 - 在主UI线程上发生了很多工作,但我该如何避免这种情况?
这是造成麻烦的代码:
private void SetupMediaElement(String mediaElementType)
{
Messenger.Default.Send("Loading video...", "SetNowWatchingVideoBusyBoxText");
Messenger.Default.Send(true, "SetNowWatchingVideoBusyBox");
try
{
if (_mainMediaElement != null)
{
VideoItem vi = CurrentSession.NowPlayingVideoItem;
if (vi != null)
{
CurrentVideoItem = vi;
MustShowImage = true;
if (vi.ID != string.Empty)
{
String mediaId = String.Empty;
if (vi.LocalFilePath != DEMOVIDEOPATH)
{
if (vi.LocalFilePath != String.Empty)
{
var fi =
new FileInfo(vi.LocalFilePath);
if (fi.Exists)
{
mediaId = fi.Name.Substring(fi.Name.LastIndexOf('-') + 1,
(fi.Name.LastIndexOf('.') -
(fi.Name.LastIndexOf('-') + 1)));
}
}
else
{
Debug.WriteLine("localFilePath is empty");
}
Debug.WriteLine("MediaId = " + mediaId +
", SessionId = " +
CurrentSession.LoggedOnUser.SessionId +
",Ticket = " +
CurrentSession.LoggedOnUser.Ticket);
string licenseURL = GetLicenseURL(mediaId, CurrentSession.LoggedOnUser.SessionId,
CurrentSession.LoggedOnUser.Ticket);
if (licenseURL != string.Empty)
{
var la = new LicenseAcquirer
{
LicenseServerUriOverride
=
new Uri(
licenseURL)
};
la.AcquireLicenseCompleted += la_AcquireLicenseCompleted;
_mainMediaElement.LicenseAcquirer = la;
}
var fileInfo = new FileInfo(vi.LocalFilePath);
string playURL = @"file://" +
Path.Combine(CoreConfig.HOME_FULL_PATH, fileInfo.Name);
playURL = playURL.Replace("\\", @"/");
VideoURL = playURL;
}
else
{
VideoURL = vi.LocalFilePath;
Messenger.Default.Send(false, "SetNowWatchingVideoBusyBox");
}
_totalDurationSet = false;
TotalTime = FormatTextHoursMinutesSecond(_mainMediaElement.NaturalDuration.TimeSpan);
SetSliderPosition();
}
}
else
{
Messenger.Default.Send(false, "SetNowWatchingVideoBusyBox");
}
}
else
{
Messenger.Default.Send(false, "SetNowWatchingVideoBusyBox");
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
VideoURL = DEMOVIDEOPATH;
Messenger.Default.Send(false, "SetNowWatchingVideoBusyBox");
}
}
感谢
编辑: 所以事实证明,上面贴的方法不是延迟的原因 - 该代码在一秒钟内执行。当媒体元素的源被设置并且它将文件读到最后时,问题就出现了 - 大文件需要时间,这就是延迟。我们在此基础上提出一个新问题。