Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 23:19, 28 June 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Audio concatenation in Windows Phone

From Wiki
Jump to: navigation, search

This article demonstrates how to concatenate audio data in Windows Phone.

WP Metro Icon Multimedia.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
Devices(s): Nokia Lumia 820,920
Platform(s): Windows Phone
Windows Phone 8
Windows Phone 7.5
Platform Security
Signing Required: Self-Signed
Keywords: Multimedia, SoundEffect
Created: somnathbanik (15 Apr 2013)
Last edited: hamishwillee (28 Jun 2013)



In this example we will record voice message through device Microphone and will append the message at the end of an audio track. Both the voice message and the audio track are of WAV format. Steps we will follow:

  • Play the audio track from the device application folder
  • Record voice message using device microphone
  • Preview the recorded voice message
  • Append the recorded voice message at the end of the audio track
  • Play the final track with the message


Create a standard Windows Phone Project

Let’s create a standard Windows Phone Application Project

  • Launch Visual Studio
  • Click on File
  • Click on New Project
  • Select Windows Phone Application (Visual C# Template)
  • Add Name and Location of the project
  • Click OK to create the project.

Adding UI controls on application page

This application has a collection of Button and a MediaElement. In the project, double-click the MainPage.xaml file and add the below code in root element.

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Start recording message" HorizontalAlignment="Left" Margin="0,79,0,0" VerticalAlignment="Top" Width="456" Click="Button_Click_2"/>
<Button Content="Stop recording message" HorizontalAlignment="Left" Margin="0,151,0,0" VerticalAlignment="Top" Width="456" Click="Button_Click_3"/>
<Button Content="Preview message" HorizontalAlignment="Left" Margin="0,223,0,0" VerticalAlignment="Top" Width="456" Click="Button_Click_4"/>
<Button Content="Append message to audio" HorizontalAlignment="Left" Margin="0,300,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.215,0.276" Width="456" Click="Button_Click_5"/>
<Button Content="Play audio with message" HorizontalAlignment="Left" Margin="0,377,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.463,0.52" Width="456" Click="Button_Click_6"/>
<MediaElement Name="MediaPlayer" HorizontalAlignment="Left" Height="0" Margin="10,454,0,0" VerticalAlignment="Top" Width="0"/>
<Button Content="Play audio" HorizontalAlignment="Left" Margin="12,10,0,0" Grid.Row="1" VerticalAlignment="Top" RenderTransformOrigin="0.345,0.1" Width="456" Click="Button_Click_1"/>

Adding references

  • Open the MainPage.xaml.cs file and add the following using statements:
using System.Windows.Threading;
using Microsoft.Xna.Framework.Audio;
using System.IO;
using Microsoft.Xna.Framework;
using System.IO.IsolatedStorage;
using System.Threading;
using Microsoft.Xna.Framework.Media.PhoneExtensions;
using Microsoft.Xna.Framework.Media;
  • Add the following declarations to the MainPage class:
        private Microphone microphone = Microphone.Default;
private byte[] buffer;
private MemoryStream stream = new MemoryStream();
private SoundEffectInstance soundInstance;
private bool soundIsPlaying = false;
public byte[] byteArrayMessage { get; set; }
public byte[] byteArrayAudio { get; set; }
public byte[] ConcatMessage { get; set; }
  • Add the following initialization in the MainPage constructor:
public MainPage()
microphone.BufferReady += new EventHandler<EventArgs>(microphone_BufferReady);
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(33);
dt.Tick += new EventHandler(dt_Tick);
  • Add the following dt_Tick and microphone_BufferReady method to the MainPage class:
void dt_Tick(object sender, EventArgs e)
try { FrameworkDispatcher.Update(); }
catch { }
if (true == soundIsPlaying)
if (soundInstance.State != SoundState.Playing)
soundIsPlaying = false;
void microphone_BufferReady(object sender, EventArgs e)
stream.Write(buffer, 0, buffer.Length);

microphone_BufferReady get the microphone buffer and writes to stream.

  • Add the following code inside a Button event to record the voice message using microphone.
//record message
private void Button_Click_2(object sender, RoutedEventArgs e)
// Get audio data in 1/2 second chunks
microphone.BufferDuration = TimeSpan.FromMilliseconds(500);
// Allocate memory to hold the audio data
buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
// Set the stream back to zero in case there is already something in it
WriteWavHeader(stream, microphone.SampleRate);
// Start recording

WriteWavHeader method defines the header structure of the recorded message.

  • Add the following code to concat the recorded message at the end of the audio track
private void Button_Click_5(object sender, RoutedEventArgs e)
byteArrayMessage = stream.ToArray(); // get recorded stream into byte array
//get audio into byte array
Stream input = new MemoryStream();
input = App.GetResourceStream(new Uri("/AudioConcat;component/Audio/musicsample.wav", UriKind.RelativeOrAbsolute)).Stream;
byteArrayAudio = ReadToEnd(input);
ConcatMessage = new byte[byteArrayAudio.Length + byteArrayMessage.Length];
System.Buffer.BlockCopy(byteArrayAudio, 0, ConcatMessage, 0, byteArrayAudio.Length);
System.Buffer.BlockCopy(byteArrayMessage, 0, ConcatMessage, byteArrayAudio.Length, byteArrayMessage.Length);
MessageBox.Show("Process completed!");

ReadToEnd method converts stream to byte array.

  • Add the following code to play the final track with the recorded message
private void Button_Click_6(object sender, RoutedEventArgs e)
SoundEffect sound = new SoundEffect(ConcatMessage, microphone.SampleRate, AudioChannels.Mono);
soundInstance = sound.CreateInstance();
soundIsPlaying = true;


To run this example with other audio file make sure the sample rate and channel of both the audio file and the recorded message are same or else you might have a distorted sound track experience.

Source Code

This page was last modified on 28 June 2013, at 23:19.
59 page views in the last 30 days.