public 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(); // Stop timer _timer.Stop(); _statusTimer.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); // 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); } } StoreEntry(memoInfo); }
/// <summary> /// Stop the record process. The recorded audio data can be get by the event AudioRecordStopped. /// </summary> /// <param name="isRecordCancelled">True for cancel this record, or false otherwise</param> /// <param name="userToken"></param> public void StopRecord(bool isRecordCancelled, object userToken) { GetRecordedData(); _microphone.Stop(); // _dispatcherTimer.Stop(); if (!isRecordCancelled) { byte[] data = _stream.ToArray(); _sampleDuration = _microphone.GetSampleDuration(data.Length); OnRecordStopped(new AudioStreamEventArgs(data, userToken)); } _stream.Close(); _stream = null; }
private void OnBufferReady(object sender, EventArgs e) { int bytesRead = 0; byte[] buffer = new byte[1024]; while ((bytesRead = this._microphone.GetData(buffer, 0, buffer.Length)) > 0) { float peak = PcmHelper.GetPeak(buffer, bytesRead); if (_state == RecorderState.Recording && _isCapturing && _writer.CanWrite) { if ((SkipSilence && peak > 0.04) || !SkipSilence) { _size += _writer.Write(buffer, 0, bytesRead); _bytesCaptured += bytesRead; _duration = (int)_microphone.GetSampleDuration(_bytesCaptured).TotalSeconds; } } RaiseSampleReceived(peak); } }