所以我从http://archive.msdn.microsoft.com/ManagedMediaHelpers下载了样本。
我的代码使用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,它可能需要一段时间才能完全下载。
更好的想法是使用编码器对文件进行分块,并根据需要进行读取。