Using MediaComposition to add background audio to video (Windows Phone 8.1)

One of our friends on the forum pointed out that we really don’t have any good samples on how to use the MediaComposition. Hopefully we will remedy this soon. To help you get your feet wet with this cool new technology added to Windows Phone 8.1 here is some really basic sample code. Enjoy!

 // Load video file
var uri = new Uri("ms-appx:///Assets/bear.mp4");
var vid = await StorageFile.GetFileFromApplicationUriAsync(uri);
 
// Load audio file
uri = new Uri("ms-appx:///Assets/song.mp3");
var aud = await StorageFile.GetFileFromApplicationUriAsync(uri);
 
// Create our output file
StorageFolder storageFolder = KnownFolders.VideosLibrary;
var file = await storageFolder.CreateFileAsync("out.mp4", CreationCollisionOption.ReplaceExisting);
 
// Create our editor
MediaComposition mc = new MediaComposition();
 
// Add our audio and backgropund audio track
mc.Clips.Add(await MediaClip.CreateFromFileAsync(vid));
mc.BackgroundAudioTracks.Add(await BackgroundAudioTrack.CreateFromFileAsync(aud));
 
// Generate the edited file
await mc.RenderToFileAsync(file);

Note: Audio and video assets have been removed from attached project files.

MediaComposition

https://msdn.microsoft.com/en-us/library/windowsphone/develop/windows.media.editing.mediacomposition.aspx

MediaCompositionBackgroundAudio.zip