metro background audio c# (release preview)

Peter Daukintis

I have just updated the sample I created for this previous post metro background audio c# (consumer preview). I have re-jigged the code to work with Win 8 Release Preview and have spruced the ui up a little bit.

I also added functionality to retrieve some audio-related metadata from an audio file and displayed it in the user interface. When you select an audio file the code will retrieve a thumbnail from the system, store it in a local folder and display it using its local uri to reference it. Here’s a snippet of code to illustrate this:

using (StorageItemThumbnail storageItemThumbnail =
    await file.GetThumbnailAsync(ThumbnailMode.SingleItem, 500, ThumbnailOptions.ResizeThumbnail))
using (IInputStream inputStreamAt = storageItemThumbnail.GetInputStreamAt(0))
using (var dataReader = new DataReader(inputStreamAt))
    uint u = await dataReader.LoadAsync((uint)storageItemThumbnail.Size);
    IBuffer readBuffer = dataReader.ReadBuffer(u);

    var tempFolder = ApplicationData.Current.LocalFolder;
    var imageFile = await tempFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

    using (IRandomAccessStream randomAccessStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite))
    using (IOutputStream outputStreamAt = randomAccessStream.GetOutputStreamAt(0))
        await outputStreamAt.WriteAsync(readBuffer);
        await outputStreamAt.FlushAsync();

// Finally, set up the Media Control
MediaControl.TrackName = audioTrack.Properties.Title;
MediaControl.ArtistName = audioTrack.Properties.Artist;
MediaControl.AlbumArt = audioTrack.ThumbUri;

Here are a few screenshots



After selecting a file the image and metadata are populated.




Navigating away from the app and accessing the System Media Controls.




Finally, a screen shot showing the design-time data in visual studio.




The project can be downloaded from here http://sdrv.ms/NiW22g.

3 thoughts on “metro background audio c# (release preview)

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.