Ejemplo n.º 1
0
        /// <summary>
        /// Send buffer to disk
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">Data contaning the buffer values</param>
        private void DataAvailable(object sender, WaveInEventArgs e)
        {
#if DEBUG
            Console.WriteLine("Bytes recorded: {0}", e.BytesRecorded);
#endif
            string fileName = string.Format("{0}.mp3", DateTime.Now.Ticks);

            if (options.WorkingDirectory != null)
            {
                fileName = Path.Combine(options.WorkingDirectory, fileName);
            }

            var writter = new LameMP3FileWriter(fileName, waveFormat, (int)RecordingBitRate.BITRATE_128);

            //Create a new task/thread separately to save this buffer to disk, avoid thread blockage at all cost
            Task.Factory.StartNew(() => {
                writter.WriteAsync(e.Buffer, 0, e.BytesRecorded)
                .ContinueWith(x => {
                    writter.Flush();
                    writter.Dispose();
                    writter = null;
#if DEBUG
                    Console.WriteLine("File written: {0}", fileName);
#endif
                    //TODO: report back to the cloud server that this service is running, index the fileChunk, upload to cloud storage
                });
            }, TaskCreationOptions.LongRunning);
        }