呈現IsolatedStorage中的多媒體資源

熟悉WP7程式設計的開發人員大概都知道,IsolatedStorage是我們在WP7程式設計中存放資料的儲存區,也是目前開發WP7唯一可以存放資料的位置。而資料存入IsolatedStorage中總是要讀取出來,但是不同類型的資料,在讀取時也有所不同。

舉例來說,同樣存放於IsolatedStorage中的圖檔、影音檔,在不同的情況下使用的方式也不同。若是以圖檔來說,要透過Image控制項顯示某個存放於IsolatedStorage中的圖檔,其程式碼如下:
System.IO.IsolatedStorage.IsolatedStorageFile iso = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
//開檔
var fs1 = iso.OpenFile("Pic1.jpg", System.IO.FileMode.Open);
//顯示jpg
System.Windows.Media.Imaging.BitmapImage BitmapImage1=
    new System.Windows.Media.Imaging.BitmapImage();
BitmapImage1.SetSource(fs1);
image1.Source = BitmapImage1;

如果是多媒體檔案,因為是透過MediaElement來顯示,所以程式碼就變成底下這樣:
System.IO.IsolatedStorage.IsolatedStorageFile iso = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
//開檔
var fs2 = iso.OpenFile("Nokia Lumia 800.mp4", System.IO.FileMode.Open);
//顯示mp4
mediaElement1.SetSource(fs2);
如果是使用MediaPlayerLauncher,用系統內建的撥放器來撥放位於IsolatedStorage中的影片呢? 程式碼又變成底下這樣:
Microsoft.Phone.Tasks.MediaPlayerLauncher mp = new Microsoft.Phone.Tasks.MediaPlayerLauncher();
//撥放isolatedStorage中的影片
mp.Media = new Uri("Nokia Lumia 800.mp4", UriKind.Relative);
mp.Show();

很有趣,還是很討厭? 由於使用的撥放方式不同,處理的媒體類型不同,使用的程式碼也有所不同。

在WP7當中,原則上,談到了uri,絕對位置Always是指向http遠端的,問題不大,但是如果是近端(Relative, 指向手機上),則有許多不同的位置需要存取,Content(專案中的Content)、IsolatedStorage、Embedded Resource...etc, 不同的位置存取的方式也不同...

本周五,在微軟的研討會,我們先從WP7的遠端與近端資料存取、DataBinding談起,3月另一場研討會,我們會繼續來討論這些多媒體資料存取的問題...

留言

這個網誌中的熱門文章

在POC或迷你專案中使用 LiteDB

使用 Airtable 在小型需求上取代傳統資料庫

專業的價值...

使用Qdrant向量資料庫實作語意相似度比對

精彩(且驚人)的Semantic Kernel入門範例