How can a silverlight app download and play an mp3 file from a URL?

Posted by Edward Tanguay on Stack Overflow See other posts from Stack Overflow or by Edward Tanguay
Published on 2010-03-21T13:01:29Z Indexed on 2010/03/21 13:11 UTC
Read the original article Hit count: 429

Filed under:
|
|

I have a small Silverlight app which downloads all of the images and text it needs from a URL, like this:

if (dataItem.Kind == DataItemKind.BitmapImage)
{
    WebClient webClientBitmapImageLoader = new WebClient();
    webClientBitmapImageLoader.OpenReadCompleted += new OpenReadCompletedEventHandler(webClientBitmapImageLoader_OpenReadCompleted);
    webClientBitmapImageLoader.OpenReadAsync(new Uri(dataItem.SourceUri, UriKind.Absolute), dataItem);
}
else if (dataItem.Kind == DataItemKind.TextFile)
{
    WebClient webClientTextFileLoader = new WebClient();
    webClientTextFileLoader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClientTextFileLoader_DownloadStringCompleted);
    webClientTextFileLoader.DownloadStringAsync(new Uri(dataItem.SourceUri, UriKind.Absolute), dataItem);
}

and:

void webClientBitmapImageLoader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.SetSource(e.Result);
    DataItem dataItem = e.UserState as DataItem;

    CompleteItemLoadedProcess(dataItem, bitmapImage);
}

void webClientTextFileLoader_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    DataItem dataItem = e.UserState as DataItem;
    string textFileContent = e.Result.ForceWindowLineBreaks();
    CompleteItemLoadedProcess(dataItem, textFileContent);
}

Each of the images and text files are then put in a dictionary so that the application has access to them at any time. This works well.

Now I want to do the same with mp3 files, but all information I find on the web about playing mp3 files in Silverlight shows how to embed them in the .xap file, which I don't want to do since I wouldn't be able to download them dynamically as I do above.

How can I download and play mp3 files in Silverlight like I download and show images and text?

© Stack Overflow or respective owner

Related posts about silverilght

Related posts about mp3