SSME不从缓存加载

时间:2012-04-05 06:56:03

标签: silverlight c#-4.0

我正在使用SSME在我的应用程序中播放视频...我甚至使用ISmoothStreamingCache接口将块存储在缓存中...但是一旦缓存完成,如果我重新启动播放视频它不会拉来自cache的数据..不是向服务器发送请求...... 任何人都可以帮助我...

命名空间CacheDemo {     public partial class MainPage:UserControl     {         ISO_StorageCache cache = null;         double sliderProportion,mediaProportionSeconds;         double sliderLengthSeconds;         双转换;         TimeSpan tsPositionFromSlider = new TimeSpan();         公共MainPage()         {             的InitializeComponent();             SmoothPlayer.CurrentStateChanged + =                     new RoutedEventHandler(SmoothPlayer_CurrentStateChanged);             SmoothPlayer.Loaded + = new RoutedEventHandler(SmoothPlayer_Loaded);             SmoothPlayer.SmoothStreamingErrorOccurred + =                 new EventHandler(SmoothPlayer_SmoothStreamingErrorOccurred);         }         void SmoothPlayer_SmoothStreamingErrorOccurred(object sender,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) { ;  }
    }

}

}

1 个答案:

答案 0 :(得分:0)

我认为您的应用程序实现看起来正确。基本上,ISmoothStreamingCache的工作方式是调用BeginRetrieve / EndRetrieve来查看您的视频是否被缓存,如果是,则从本地缓存中检索它。否则,在线获取视频后,它会调用BeginPersist和EndPersist将您的视频保存到本地存储中。

由于您无法从缓存中获取视频,因此我怀疑您的缓存可能已满。你能在Endpersist方法中设置一个断点并查找这一行:      if(((CacheResponse)(((CacheAsyncResult)ar).Result))。Response.Length&lt; isoFileArea.AvailableFreeSpace)

同时,尝试清除浏览器中的缓存。如果你不跳进这个if块,你也可以增加Quota并尝试。

我有关于此主题的博客,您可以查看一下。希望它能解决您的问题:http://blogs.msdn.com/b/mingfeis_code_block/archive/2012/04/10/smooth-streaming-cache-plug-in-implementation.aspx