2012-04-05 8 views
0

私のアプリケーションでビデオを再生するためにSSMEを使用しています...私はISmoothStreamingCacheインターフェイスを使ってキャッシュにチャンクを格納しています...しかし、ユーザーコントロール { ISO_StorageCache:ビデオそれは は誰でもこの上で私を助けることができる...SSMEがキャッシュからロードされない

名前空間CacheDemo { 公共部分クラスメインページ... cache..insteadからのデータをサーバに要求を送信引っ張っていませんキャッシュ=ヌル; double sliderPropertion、mediaProportionSeconds; double sliderLengthSeconds; 二重変換; TimeSpan tsPositionFromSlider = new TimeSpan(); 公開MainPage() { InitializeComponent(); SmoothPlayer.CurrentStateChanged + = 新しいRoutedEventHandler(SmoothPlayer_CurrentStateChanged); SmoothPlayer.Loaded + =新しいRoutedEventHandler(SmoothPlayer_Loaded); SmoothPlayer.SmoothStreamingErrorOccurred + = 新しいEventHandler(SmoothPlayer_SmoothStreamingErrorOccurred); } void SmoothPlayer_SmoothStreamingErrorOccurred(オブジェクト送信者、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) { ; } 
    } 

} 

}

答えて

0

私はあなたのアプリケーションの実装が右に見えると思います。基本的に、ISmoothStreamingCacheの動作はBeginRetrieve/EndRetrieveが呼び出され、ビデオがキャッシュされているかどうかを確認します。それ以外の場合は、ビデオをオンラインで取得した後、BeginPersistとEndPersistを呼び出してビデオをローカルストレージに保存します。

キャッシュからビデオを取得できなかったため、キャッシュがいっぱいである可能性があります。あなたはEndpersistメソッドにブレークポイントを設定し、この行を探しでした:(((CacheResponse)(((CacheAsyncResult)AR).Result))Response.Length < isoFileArea.AvailableFreeSpace)場合

一方、にしてみてくださいブラウザのキャッシュをクリアしてください。このブロックにジャンプしないと、あなたはまたクォータを試して試すことができます。

私はこのトピックに関するブログを持っており、あなたはそれをチェックアウトすることができます。あなたの問題を解決することを願って:http://blogs.msdn.com/b/mingfeis_code_block/archive/2012/04/10/smooth-streaming-cache-plug-in-implementation.aspx

+0

あなたはブログのエントリが何を言っているか教えていただけますか? –

関連する問題