我正在使用 SSME 在我的应用程序中播放视频...我什至使用 ISmoothStreamingCache 接口将块存储在缓存中...但是一旦缓存完成,如果我重新启动播放视频,它就不会从缓存中提取数据..而不是向服务器发送请求...任何人都可以帮助我...
命名空间 CacheDemo { 公共部分类 MainPage : UserControl { ISO_StorageCache 缓存 = null; 双滑块比例,媒体比例秒;双滑块长度秒;双重转换;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) { ; }
}
}
}