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.

Audio concatenation in Windows Phone (Arabic)

From Wiki
Jump to: navigation, search
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
By bintk
Last edited: hamishwillee (28 Jun 2013)



في هذا المثال سنقوم بتسجيل رسالة صوتية من خلال ميكروفون الجهاز وسيتم إلحاق هذه الرسالة في نهاية أحدي المسارات الصوتية. وسيتم حفظ الرسالة الصوتية والمسار الصوتي علي شكل WAV

  • وسوف نتابع الخطوات:
  • تشغيل المسار الصوتي من مجلد التطبيق الجهاز
  • الرسالة الصوتية سجل باستخدام ميكروفون الجهاز
  • معاينة الرسالة الصوتية المسجلة
  • إلحاق الرسالة الصوتية المسجلة في نهاية المسار الصوتي
  • تشغيل المسار النهائي مع رسالة


نشئ تطبيق ويندوز فون جديد

أضف عناصر التحكم التالية إلي واجهة التطبيق

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;


لتشغيل هذا المثال مع ملفات صوتية أخري تأكد معاينه معدل الصوت لهذا الملف و الرساله المسجلة يجب ان تكون بنفس المعدل و المستوي حتي تحصل على نتائج مرضية .

الكود المصدري

  • لتحميل الكود المصدري لذها المثال أنقر هنا :
This page was last modified on 28 June 2013, at 23:19.
85 page views in the last 30 days.