Silverlight SSME不';无法从缓存中加载

Silverlight SSME不';无法从缓存中加载,silverlight,c#-4.0,Silverlight,C# 4.0,我在应用程序中使用SSME播放视频…我甚至使用ISmoothStreamingCache接口将块存储在缓存中…但一旦缓存完成,如果我重新启动播放视频,它不会从缓存中提取数据..而是将请求发送到服务器。。。 有人能帮我吗 名称空间缓存演示 { 公共部分类主页面:UserControl { ISO_StorageCache=null; 双滑块比例,中间比例秒; 双滑块长度秒; 双转换; TimeSpan tsPositionFromSlider=新的TimeSpan(); 公共主页() { 初始化组

我在应用程序中使用SSME播放视频…我甚至使用ISmoothStreamingCache接口将块存储在缓存中…但一旦缓存完成,如果我重新启动播放视频,它不会从缓存中提取数据..而是将请求发送到服务器。。。 有人能帮我吗

名称空间缓存演示 { 公共部分类主页面:UserControl { ISO_StorageCache=null; 双滑块比例,中间比例秒; 双滑块长度秒; 双转换; TimeSpan tsPositionFromSlider=新的TimeSpan(); 公共主页() { 初始化组件(); SmoothPlayer.CurrentStateChanged+= 新RoutedEventHandler(SmoothPlayer_CurrentStateChanged); SmoothPlayer.Loaded+=新的路由EventHandler(SmoothPlayer_Loaded); SmoothPlayer.SmoothStreamingError发生+= 新事件处理程序(SmoothPlayer\u SmoothStreamingErrorOccessed); } 无效SmoothPlayer\u SmoothStreamingErrorOccursed(对象发送方,SmoothStreamingErrorEventArgs e) { OutputText.Text=e.ErrorMessage.ToString(); }

    void SmoothPlayer_Loaded(object sender, RoutedEventArgs e)
    {
        cache = new ISO_StorageCache();
        SmoothPlayer.SmoothStreamingSource = new Uri("http://<serverName>/media1.ism/Manifest");

       // SmoothPlayer.SmoothStreamingCache = cache;            
    }

    private void SliderBar_ValueChanged(object sender,
                   RoutedPropertyChangedEventArgs<double> e)
    {
        try
        {
            // Calculate proportion of slider length at current position.
            sliderProportion = ((Slider)sender).Value / ((Slider)sender).Maximum;
            // Get media length in seconds.
            sliderLengthSeconds = SmoothPlayer.EndPosition.TotalSeconds;
            // Calculate position in seconds.
            conversion = sliderProportion * sliderLengthSeconds;
            // Convert seconds to a TimeSpan object.
            tsPositionFromSlider = TimeSpan.FromSeconds(conversion);
            // Assign new position by TimeSpan.
            SmoothPlayer.Position = tsPositionFromSlider;
        }
        catch (Exception ex)
        {
            OutputText.Text = ex.Message;
        }
    }

    void SmoothPlayer_CurrentStateChanged(object sender, RoutedEventArgs e)
    {
        switch (SmoothPlayer.CurrentState)
        {
            case SmoothStreamingMediaElementState.Playing:
                PlayButton.Content = "Pause";
                break;

            case SmoothStreamingMediaElementState.Paused:
                PlayButton.Content = "Play";
                break;

            case SmoothStreamingMediaElementState.Stopped:
                PlayButton.Content = "Play";
                break;
        }
    }

    private void SourceList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListBoxItem item = ((sender as ComboBox).SelectedItem as ListBoxItem);
        string selection = item.Content as string;

        switch (selection)
        {
            case "Lewis Clark clip":

                SmoothPlayer.SmoothStreamingSource = new Uri("http://<serverName>/media1.ism/Manifest");
                break;
            case "Media Two":
                SmoothPlayer.SmoothStreamingSource = null;
                //new Uri("http://<serverName>/media2.ism/Manifest");
                break;
            case "Media Three":
                SmoothPlayer.SmoothStreamingSource =
                    new Uri("http://<serverName>/media3.ism/Manifest");
                break;
            case "Media Four":
                SmoothPlayer.SmoothStreamingSource =
                    new Uri("http://<serverName>/media4.ism/Manifest");
                break;

        }
    }

    private void PlayButton_Loaded(object sender, RoutedEventArgs e)
    {
        switch (SmoothPlayer.AutoPlay)
        {
            case false:
                PlayButton.Content = "Play";
                break;
            case true:
                PlayButton.Content = "Pause";
                break;
        }
    }

    private void PlayButton_Click(object sender, RoutedEventArgs e)
    {
        switch (SmoothPlayer.CurrentState)
        {
            case SmoothStreamingMediaElementState.Playing:
                SmoothPlayer.Pause();
                PlayButton.Content = "Play";
                break;

            case SmoothStreamingMediaElementState.Paused:
                SmoothPlayer.Play();
                PlayButton.Content = "Pause";
                break;

            case SmoothStreamingMediaElementState.Stopped:
                SmoothPlayer.Play();
                PlayButton.Content = "Pause";
                break;
        }
    }

    private void StopButton_Click(object sender, RoutedEventArgs e)
    {
        if (SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Playing)
            SmoothPlayer.Stop();
        PlayButton.Content = "Play";
    }

    private void clearCacheButton_Click(object sender, RoutedEventArgs e)
    {
        cache.keyUrls.Clear();
        IsolatedStorageSettings.ApplicationSettings.Clear();
        IsolatedStorageSettings.SiteSettings.Clear();

        IsolatedStorageFile isoFileArea = IsolatedStorageFile.GetUserStoreForApplication();
        string[] names = isoFileArea.GetFileNames();

        foreach (string file in names)
        {
            isoFileArea.DeleteFile(file);
        }

    }

    private void increaseQuotaButton_Click(object sender, RoutedEventArgs e)
    {
        IsolatedStorageFile isoFileArea = IsolatedStorageFile.GetUserStoreForApplication();
        long availbleCacheBytes = isoFileArea.AvailableFreeSpace;
        long saveQuotaNumber = isoFileArea.Quota;

        isoFileArea.IncreaseQuotaTo(isoFileArea.Quota + 1048576);

        MessageBox.Show("New Quota: " + isoFileArea.Quota.ToString() +
            "\r\nPrevious Quota: " + saveQuotaNumber.ToString() +
            "\r\nAvailable Free Space: " + isoFileArea.AvailableFreeSpace.ToString(), "Cache Quota",
            MessageBoxButton.OK);
    }

    private void cacheOrNot_Click(object sender, RoutedEventArgs e)
    {
        if ("Not Caching" == (string)cacheOrNot.Content)
        {
            SmoothPlayer.SmoothStreamingCache = cache;
            cacheOrNot.Content = "Caching On";
        }
        else if ("Caching On" == (string)cacheOrNot.Content)
        {
            SmoothPlayer.SmoothStreamingCache = null;
            cacheOrNot.Content = "Not Caching";
        }
    }

}
public class ISO_StorageCache : ISmoothStreamingCache
{
    // Dictionary to track URL/filename pairs of data in cache.
    public Dictionary<string, string> keyUrls = new Dictionary<string, string>(50);

    public ISO_StorageCache()
    {
        IsolatedStorageFile isoFileArea = IsolatedStorageFile.GetUserStoreForApplication();

        foreach (System.Collections.Generic.KeyValuePair<string, object> pair in IsolatedStorageSettings.ApplicationSettings)
        {
            if (!keyUrls.ContainsValue((string)pair.Value) && isoFileArea.FileExists((string)pair.Value))
                keyUrls.Add(pair.Key, ((string)pair.Value));
        }
    }

    public IAsyncResult BeginPersist(CacheRequest request, CacheResponse response, AsyncCallback callback, object state)
    {
        state = false;
        CacheAsyncResult ar = new CacheAsyncResult();

        if (!keyUrls.ContainsKey(request.CanonicalUri.ToString()))
        {
            //state = true;
            ar.strUrl = request.CanonicalUri.ToString();
            ar.Complete(response, true);
            return ar;
        }

        ar.Complete(null, true);
        return ar;
    }

    public bool EndPersist(IAsyncResult ar)
    {
        ar.AsyncWaitHandle.WaitOne();

        if (((CacheAsyncResult)ar).Result != null)
        {
            IsolatedStorageFile isoFileArea = IsolatedStorageFile.GetUserStoreForApplication();
            //const long NEEDED = 1024 * 1024 * 100;
            //isoFileArea.IncreaseQuotaTo(isoFileArea.Quota + NEEDED);
            if (((CacheResponse)(((CacheAsyncResult)ar).Result)).Response.Length < isoFileArea.AvailableFreeSpace)
            {
                string fileGuid = Guid.NewGuid().ToString();

                if (!keyUrls.ContainsValue(fileGuid) && !keyUrls.ContainsKey(((CacheAsyncResult)ar).strUrl))
                {

                    IsolatedStorageFileStream isoFile = isoFileArea.CreateFile(fileGuid);

                    ((CacheResponse)(((CacheAsyncResult)ar).Result)).WriteTo(isoFile);
                    isoFile.Close();

                    keyUrls.Add(((CacheAsyncResult)ar).strUrl, fileGuid);
                    // Save key/value pairs for playback after application restarts.
                    IsolatedStorageSettings.ApplicationSettings.Add(((CacheAsyncResult)ar).strUrl, fileGuid);
                    IsolatedStorageSettings.ApplicationSettings.Save();

                    return true;
                }
            }
        }
        return false;
    }

    public IAsyncResult BeginRetrieve(CacheRequest request, AsyncCallback callback, object state)
    {
        CacheResponse response = null;
        CacheAsyncResult ar = new CacheAsyncResult();
        ar.strUrl = request.CanonicalUri.ToString();
        ar.Complete(response, true);
        return ar;
    }

    public CacheResponse EndRetrieve(IAsyncResult ar)
    {
        ar.AsyncWaitHandle.WaitOne();

        CacheResponse response = null;

        if (keyUrls.ContainsKey(((CacheAsyncResult)ar).strUrl))
        {
            IsolatedStorageFile isoFileArea = IsolatedStorageFile.GetUserStoreForApplication();
            string filename = keyUrls[((CacheAsyncResult)ar).strUrl];

            if (!string.IsNullOrEmpty(filename) && isoFileArea.FileExists(filename))
            {
                IsolatedStorageFileStream stream =
                    isoFileArea.OpenFile(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                response = new CacheResponse(stream);
            }
        }

        if (response != null)
            return response;
        else
            return response = new CacheResponse(0, null, null, null,
                System.Net.HttpStatusCode.NotFound, "Not Found", DateTime.Now);
    }
}

public class CacheAsyncResult : IAsyncResult
{
    public string strUrl { get; set; }

    public object AsyncState { get; private set; }

    public WaitHandle AsyncWaitHandle { get { return _completeEvent; } }

    public bool CompletedSynchronously { get; private set; }

    public bool IsCompleted { get; private set; }

    // Contains all the output result of the GetChunk API
    public Object Result { get; private set; }

    internal TimeSpan Timestamp { get; private set; }

    /// <summary>
    /// Callback function when GetChunk is completed. Used in asynchronous mode only.
    /// Should be null for synchronous mode.
    /// </summary>
    private AsyncCallback _callback;

    /// <summary>
    /// Event is used to signal the completion of the operation
    /// </summary>
    private ManualResetEvent _completeEvent = new ManualResetEvent(false);

    /// <summary>
    /// Called when the operation is completed
    /// </summary>
    public void Complete(Object result, bool completedSynchronously)
    {
        Result = result;
        CompletedSynchronously = completedSynchronously;

        IsCompleted = true;
        _completeEvent.Set();

        if (null != _callback) { ;  }
    }

}
void SmoothPlayer\u已加载(对象发送器,路由目标)
{
缓存=新的ISO_StorageCache();
SmoothPlayer.SmoothStreamingSource=新Uri(“http:///media1.ism/Manifest");
//SmoothPlayer.SmoothStreamingCache=缓存;
}
私有无效滑块Bar_值已更改(对象发送方,
RoutedPropertyChangedEventArgs(e)
{
尝试
{
//计算滑块在当前位置的长度比例。
sliderProportion=((滑块)发送器).Value/((滑块)发送器).max;
//以秒为单位获取媒体长度。
sliderLengthSeconds=SmoothPlayer.EndPosition.TotalSeconds;
//以秒为单位计算位置。
转换=滑块比例*滑块长度秒;
//将秒转换为TimeSpan对象。
tsPositionFromSlider=TimeSpan.FromSeconds(转换);
//按时间跨度分配新职位。
SmoothPlayer.Position=tsPositionFromSlider;
}
捕获(例外情况除外)
{
OutputText.Text=ex.Message;
}
}
void SmoothPlayer\u CurrentStateChanged(对象发送方,路由目标)
{
开关(SmoothPlayer.CurrentState)
{
case SmoothStreamingMediaElementState.播放:
PlayButton.Content=“暂停”;
打破
案例SmoothStreamingMediaElementState。已暂停:
PlayButton.Content=“Play”;
打破
案例SmoothStreamingMediaElementState。已停止:
PlayButton.Content=“Play”;
打破
}
}
private void SourceList_SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
ListBoxItem=((发件人作为组合框)。选择EdItem作为ListBoxItem);
字符串选择=项。内容为字符串;
开关(选择)
{
案例“刘易斯·克拉克剪辑”:
SmoothPlayer.SmoothStreamingSource=新Uri(“http:///media1.ism/Manifest");
打破
案例“媒体二”:
SmoothPlayer.SmoothStreamingSource=null;
//新Uri(“http:///media2.ism/Manifest");
打破
案例“媒体三”:
SmoothPlayer.SmoothStreamingSource=
新Uri(“http:///media3.ism/Manifest");
打破
案例“媒体四”:
SmoothPlayer.SmoothStreamingSource=
新Uri(“http:///media4.ism/Manifest");
打破
}
}
已加载专用void播放按钮(对象发送器,路由目标e)
{
开关(SmoothPlayer.AutoPlay)
{
案例错误:
PlayButton.Content=“Play”;
打破
大小写正确:
PlayButton.Content=“暂停”;
打破
}
}
私有void播放按钮(对象发送者,路由目标e)
{
开关(SmoothPlayer.CurrentState)
{
case SmoothStreamingMediaElementState.播放:
SmoothPlayer.Pause();
PlayButton.Content=“Play”;
打破
案例SmoothStreamingMediaElementState。已暂停:
SmoothPlayer.Play();
PlayButton.Content=“暂停”;
打破
案例SmoothStreamingMediaElementState。已停止:
SmoothPlayer.Play();
PlayButton.Content=“暂停”;
打破
}
}
私有无效停止按钮\单击(对象发送者,路由目标)
{
如果(SmoothPlayer.CurrentState==SmoothStreamingMediaElementState.Playing)
SmoothPlayer.Stop();
PlayButton.Content=“Play”;
}
私有无效clearCacheButton_单击(对象发送者,路由目标)
{
cache.keyUrls.Clear();
IsolatedStorageSettings.ApplicationSettings.Clear();
IsolatedStorageSettings.SiteSettings.Clear();
IsolatedStorageFile isoFileArea=IsolatedStorageFile.GetUserStoreForApplication();
字符串[]名称=isoFileArea.GetFileNames();
foreach(名称中的字符串文件)
{
isoFileArea.DeleteFile(文件);
}
}
私有无效增加QuotaButton(对象发送者,路由目标e)
{
IsolatedStorageFile isoFileArea=IsolatedStorageFile.GetUserStoreForApplication();
long AvailableCacheBytes=isoFileArea.AvailableFreeSpace;
long saveQuotaNumber=isoFileArea.Quota;
isoFileArea.IncreaseQuotaTo(isoFileArea.Quota+1048576);
Show(“新配额:+isoFileArea.Quota.ToString()+
“\r\n以前的配额:”+saveQuotaNumber.ToStri