×
Namespaces

Variants
Actions

Audio concatenation in Windows Phone (Arabic)

From Nokia Developer 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
Compatibility
Platform(s): Windows Phone
Windows Phone 8
Windows Phone 7.5
Platform Security
Signing Required: Self-Signed
Capabilities: ID_CAP_MICROPHONE
Article
Keywords: Multimedia, SoundEffect
Translated:
By bintk
Last edited: hamishwillee (28 Jun 2013)

Contents

المقدمة

في هذا المثال سنقوم بتسجيل رسالة صوتية من خلال ميكروفون الجهاز وسيتم إلحاق هذه الرسالة في نهاية أحدي المسارات الصوتية. وسيتم حفظ الرسالة الصوتية والمسار الصوتي علي شكل 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"/>
</Grid>
<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()
{
InitializeComponent();
microphone.BufferReady += new EventHandler<EventArgs>(microphone_BufferReady);
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(33);
dt.Tick += new EventHandler(dt_Tick);
dt.Start();
}
  • 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)
{
microphone.GetData(buffer);
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
stream.SetLength(0);
WriteWavHeader(stream, microphone.SampleRate);
// Start recording
microphone.Start();
}

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;
soundInstance.Play();
}

الملخص

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

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

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

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×