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.

About these ads

2 Responses to “metro background audio c# (release preview)”

  1. November 22, 2012 at 8:18 pm

    When you rotate the emulator, I get a MF_MEDIA_ENGINE_ERR_DECODE . any suggestions ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: