所以我已经从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);
}
}
实际上,这只是我需要解释的最后一种方法,对于为什么这是一个坏主意以及如何以不同的方式做事,我不了解“通知”。
最佳答案
基本上,它试图告诉您要在播放之前完全下载1个文件。这不是一个好主意,因为如果文件为10 MB,则可能需要一段时间才能完全下载。
更好的主意是使用编码器对文件进行分块,然后根据需要读取文件。
关于silverlight - WP7音频流帮助,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7009635/