public MainPage() { InitializeComponent(); // Create new Microphone and set event handler microphone = Microphone.Default; microphone.BufferReady += OnMicrophoneBufferReady; // Create new DynamicSoundEffectInstace for playback playback = new DynamicSoundEffectInstance(microphone.SampleRate, AudioChannels.Mono); playback.BufferNeeded += OnPlaybackBufferNeeded; // Enumerate existing memo waveform files in isolated storage using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) { // Show filenames with most recent first string[] filenames = storage.GetFileNames(); Array.Sort(filenames); Array.Reverse(filenames); foreach (string filename in filenames) { using (IsolatedStorageFileStream stream = storage.OpenFile(filename, FileMode.Open, FileAccess.Read)) { TimeSpan duration = microphone.GetSampleDuration((int)stream.Length); MemoInfo memoInfo = new MemoInfo(filename, stream.Length, duration); memoFiles.Add(memoInfo); } } } // Set memo collection to ListBox memosListBox.ItemsSource = memoFiles; // Set-up record button recordButton.DataContext = spaceTime; UpdateRecordButton(false); }
void StopRecording() { // Get the last partial buffer int sampleSize = microphone.GetSampleSizeInBytes(microphone.BufferDuration); byte[] extraBuffer = new byte[sampleSize]; int extraBytes = microphone.GetData(extraBuffer); // Stop recording microphone.Stop(); // Create MemoInfo object and add at top of collection int totalSize = memoBufferCollection.Count * sampleSize + extraBytes; TimeSpan duration = microphone.GetSampleDuration(totalSize); MemoInfo memoInfo = new MemoInfo(DateTime.UtcNow, totalSize, duration); memoFiles.Insert(0, memoInfo); // Save data in isolated storage using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream stream = storage.CreateFile(memoInfo.FileName)) { // Write buffers from collection foreach (byte[] buffer in memoBufferCollection) stream.Write(buffer, 0, buffer.Length); // Write partial buffer stream.Write(extraBuffer, 0, extraBytes); } } // Scroll to show new MemoInfo item memosListBox.UpdateLayout(); memosListBox.ScrollIntoView(memoInfo); }