Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WaveRecorderViewModel"/> class.
        /// </summary>
        public AudioProcessor(string file)
        {
            SampleRate  = 16000;
            IsRecording = true;
            var app = Resolver.Resolve <IXFormsApp>();

            var device = Resolver.Resolve <IDevice>();

            if (device != null)
            {
                _audioStream = device.Microphone;
                _recorder    = new WaveRecorder();
            }
            var fileStream = DependencyService.Get <IFileLoader>().LoadWriteStream(file);

            Record = new Command(
                () => {
                _audioStream.OnBroadcast += audioStream_OnBroadcast;
                //this.audioStream.Start.Execute(this.SampleRate);
                _recorder.StartRecorder(
                    _audioStream,
                    fileStream,
                    SampleRate).ContinueWith(t => {
                    if (t.IsCompleted)
                    {
                        IsRecording = t.Result;
                        System.Diagnostics.Debug.WriteLine("Microphone recorder {0}.", IsRecording ? "was started" : "failed to start.");
                        Record.ChangeCanExecute();
                        Stop.ChangeCanExecute();
                    }
                    else if (t.IsFaulted)
                    {
                        _audioStream.OnBroadcast -= audioStream_OnBroadcast;
                    }
                });
            },
                () => RecordingEnabled &&
                _audioStream.SupportedSampleRates.Contains(SampleRate) &&
                !IsRecording &&
                device.FileManager != null
                );

            Stop = new Command(
                async() => {
                _audioStream.OnBroadcast -= audioStream_OnBroadcast;
                await _recorder.StopRecorder();
                //this.audioStream.Stop.Execute(this);
                Debug.WriteLine("Microphone recorder was stopped.");
                Record.ChangeCanExecute();
                Stop.ChangeCanExecute();
                IsRecording = false;
            },
                () => {
                return(IsRecording);
            }
                );
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="WaveRecorderViewModel"/> class.
        /// </summary>
        public WaveRecorderViewModel()
        {
            SampleRate = 16000;

            var app = Resolver.Resolve <IXFormsApp>();

            //this.FileName = System.IO.Path.Combine(app.AppDataDirectory, "audiosample.wav");
            FileName = Device.OnPlatform(
                System.IO.Path.Combine(app.AppDataDirectory, "audiosample.wav"),
                "audiosample.wav",
                System.IO.Path.Combine(app.AppDataDirectory, "audiosample.wav")
                );

            var device = Resolver.Resolve <IDevice>();

            if (device != null)
            {
                _audioStream = device.Microphone;
                _recorder    = new WaveRecorder();
            }

            Record = new Command(
                () =>
            {
                _audioStream.OnBroadcast += audioStream_OnBroadcast;
                //this.audioStream.Start.Execute(this.SampleRate);
                _recorder.StartRecorder(
                    _audioStream,
                    device.FileManager.OpenFile(FileName, Platform.Services.IO.FileMode.Create, Platform.Services.IO.FileAccess.Write),
                    SampleRate).ContinueWith(t =>
                {
                    if (t.IsCompleted)
                    {
                        IsRecording = t.Result;
                        System.Diagnostics.Debug.WriteLine("Microphone recorder {0}.", IsRecording ? "was started" : "failed to start.");
                        Record.ChangeCanExecute();
                        Stop.ChangeCanExecute();
                    }
                    else if (t.IsFaulted)
                    {
                        _audioStream.OnBroadcast -= audioStream_OnBroadcast;
                    }
                });
            },
                () => RecordingEnabled &&
                _audioStream.SupportedSampleRates.Contains(SampleRate) &&
                !IsRecording &&
                device.FileManager != null
                );

            Stop = new Command(
                async() =>
            {
                _audioStream.OnBroadcast -= audioStream_OnBroadcast;
                await _recorder.StopRecorder();
                //this.audioStream.Stop.Execute(this);
                System.Diagnostics.Debug.WriteLine("Microphone recorder was stopped.");
                Record.ChangeCanExecute();
                Stop.ChangeCanExecute();
            },
                () =>
            {
                return(IsRecording);
            }
                );
        }