Question creating audio recording app, recordings get appended

Spikey

New member
Joined
Aug 9, 2012
Messages
1
Programming Experience
Beginner
I am creating a Windows Phone travel app that also allows users to record audio fragments, these are then saved to isolated storage and are retrieved when a user requests them. I have coded the page that does the recording, saving to storage and playback but the problem is when I make a recording and then make a second recording the second gets appended to the first and so on, so when I playback I hear all the recordings one after the other, this isn't what I want, I should only hear the last recording they made as the idea is that only one audio fragment should be saved. Below is the code for the page to add audio, any advice appreciated.


        public AddAudio()
        {
            InitializeComponent();
            ApplicationTitle.Text = "AUDIO RECORDER";
            PageTitle.Text = "ready";
            microphone.BufferReady += (object sender, EventArgs e) =>
            {
                microphone.GetData(buffer);
                stream.Write(buffer, 0, buffer.Length);
            };
        }
 
       private void recordButton_Click(object sender, RoutedEventArgs e)
        {
            if (recording == false)
            {
                microphone.BufferDuration = TimeSpan.FromMilliseconds(100);
                buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferD  uration)];
                WaveHelper.WriteWaveHeader(stream, microphone.SampleRate);
                recording = true;
            }
            microphone.Start();
            PageTitle.Text = "record";         
        }
 
        private void stopRecordButton_Click(object sender, RoutedEventArgs e)
        {
            if (microphone.State == MicrophoneState.Started)
            {
                microphone.Stop();
                WaveHelper.UpdateWaveHeader(stream);
                recording = false;
                PageTitle.Text = "stop"; 
            }
 
            fileName = Guid.NewGuid().ToString();
 
            url = "Audio\'" + fileName;
 
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
 
                if (!store.DirectoryExists("Audio"))
                {
                    store.CreateDirectory("Audio");
                }
 
                if (store.FileExists(url))
                {
                    store.DeleteFile(url);
                }
 
                using (IsolatedStorageFileStream isoStream = store.OpenFile(@url, FileMode.OpenOrCreate))
                {
                    isoStream.Write(stream.ToArray(), 0, stream.ToArray().Length);
                    isoStream.Close();
 
                }
            }
            //stream.Close();
        }
 
        private void playButton_Click(object sender, RoutedEventArgs e)
        {
            SoundEffect se;
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isoStream = store.OpenFile(@url, FileMode.Open))
                {
                    byte[] b = new byte[isoStream.Length];
                    isoStream.Read(b, 0, b.Length);
                    se = new SoundEffect(b.ToArray(), microphone.SampleRate, AudioChannels.Mono);
                }
            }
            se.Play();
            PageTitle.Text = "play"; 
        }
 
Last edited by a moderator:
Back
Top Bottom