0

アプリケーションでビデオを再生するためにSSMEを使用しています...キャッシュにチャンクを保存するためにISmoothStreamingCacheインターフェイスを使用しました...しかし、キャッシュが完了したら、再生中のビデオを再起動すると、キャッシュからデータがプルされません..代わりにサーバーにリクエストを送信してください...誰か助けてくれませんか...

名前空間 CacheDemo { public partial class MainPage : UserControl { ISO_StorageCache キャッシュ = null; ダブルスライダーProportion、mediaProportionSeconds; double sliderLengthSeconds; 二重変換; TimeSpan tsPositionFromSlider = new TimeSpan(); public 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) { ;  }
    }

}

}

4

1 に答える 1

0

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

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

その間、ブラウザのキャッシュをクリアしてみてください。この if ブロックに飛び込まない場合は、Quota を増やして試すこともできます。

このトピックに関するブログがありますので、ぜひご覧ください。問題が解決することを願っています: http://blogs.msdn.com/b/mingfeis_code_block/archive/2012/04/10/smooth-streaming-cache-plug-in-implementation.aspx

于 2012-05-31T18:16:46.650 に答える