2011-08-10 105 views
1

所以我從http://archive.msdn.microsoft.com/ManagedMediaHelpers下載了樣本。WP7音頻串流幫助

我有我的代碼工作使用MP3MediaStreamSource。但是,我不完全理解代碼會想要一些解釋。

public partial class MainPage : PhoneApplicationPage 
{ 
    private static string mediaFileLocation = "http://file-here.mp3"; 
    private static HttpWebRequest request = null; 
    private static Mp3MediaStreamSource mss = null; 

    public MainPage() 
    { 
     InitializeComponent(); 
    } 

    private void RequestCallback(IAsyncResult asyncResult) 
    { 
     HttpWebResponse response = request.EndGetResponse(asyncResult) as HttpWebResponse; 
     Stream s = response.GetResponseStream(); 
     mss = new Mp3MediaStreamSource(s, response.ContentLength); 
     Deployment.Current.Dispatcher.BeginInvoke(
      () => 
      { 
       this.wp7AudioElement.Volume = 100; 
       this.wp7AudioElement.SetSource(mss); 
      }); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     request = WebRequest.CreateHttp(MainPage.mediaFileLocation); 

     // NOTICE 
     // Makes this demo code easier but I wouldn't do this on a live phone as it will cause the whole 
     // file to download into memory at once. 
     // 
     // Instead, use the asynchronous methods and read the stream in the backgound and dispatch its 
     // data as needed to the ReportGetSampleCompleted call on the UIThread. 
     request.AllowReadStreamBuffering = true; 
     IAsyncResult result = request.BeginGetResponse(new AsyncCallback(this.RequestCallback), null); 
    } 
} 

這真的只是最後的方法我需要解釋的,我不明白的通知,爲什麼這是一個壞主意,如何做到這一點不同?

+0

你知道如何做到這一點嗎? –

回答

0

基本上,它試圖告訴你,你正在下載1個文件完全播放之前。這不是一個好主意,因爲如果該文件是10 MB,則可能需要一段時間才能完全下載。

一個更好的想法是使用編碼器對文件進行分塊並在需要的基礎上讀取它。