private void Connection_StatusChanged(object sender, ConnectionStatusEventArgs e) { if (e.Reason == ConnectionStatus.Connected) { IsConnected = true; TextPopUp.Dismiss(); _onConnectedCallback(); if (responseHandler == null) { responseHandler = new ResponseHandler(); } } if (e.Reason == ConnectionStatus.ConnectionClosed || e.Reason == ConnectionStatus.ConnectionLost || e.Reason == ConnectionStatus.Unknown) { IsConnected = false; _connection.Dispose(); TextPopUp.Text = "Connection lost. Trying to reconnect in 5s."; TextPopUp.Show(); MainPage.SetActionButtonIsEnabled(false); MainPage.SetButtonImage("listen_disabled_allgreyedout.png"); if (AudioRecorder.IsRecording) { AudioRecorder.StopRecording(); } //if (AudioPlayer.IsPlaying) //{ // AudioPlayer.Stop(); //} _connection.DataReceived -= Connection_DataReceived; _connection.StatusChanged -= Connection_StatusChanged; _connection.Close(); _connection = null; _peer = null; _agent = null; _reconnectTimer.Start(); } }
private void Connection_DataReceived(object sender, Samsung.Sap.DataReceivedEventArgs e) { AssistResponse ar = AssistResponse.Parser.ParseFrom(e.Data); if (ar.SpeechResults != null) { if (ar.SpeechResults.Any() && ar.SpeechResults.First().Stability > 0.01) { label.Text = ar.SpeechResults.First().Transcript; if (ar.SpeechResults.First().Stability == 1) { audioRecorder.StopRecording(); updateLabel(ar.SpeechResults.First().Transcript); if (File.Exists(filePath)) { File.Delete(filePath); } fs = File.Create(filePath); } } } if (ar.DialogStateOut != null && ar.DialogStateOut.SupplementalDisplayText != "") { updateLabel(ar.DialogStateOut.SupplementalDisplayText); } if (ar.DialogStateOut != null && ar.DialogStateOut.VolumePercentage != 0) { int newVolumeLevel = Convert.ToInt32(15 * ar.DialogStateOut.VolumePercentage / 100); AudioManager.VolumeController.Level[AudioVolumeType.Media] = newVolumeLevel; actionButton.IsEnable = true; } if (ar.ScreenOut != null) { updateLabel(ar.ScreenOut.Data.ToStringUtf8()); } if (ar.AudioOut != null && ar.AudioOut.AudioData.Length != 0) { try { fs.Write(ar.AudioOut.AudioData.ToByteArray(), 0, ar.AudioOut.AudioData.Length); if (fs.Length != 0) { fs.Flush(); } if (!isPlaying && fs.Length >= 1600) { isPlaying = true; audioPlayer.Play(fs.Name); actionButton.IsEnable = true; actionButton.BackgroundColor = Color.Red; actionButton.Text = "Stop"; } } catch (System.ObjectDisposedException) { Tizen.Log.Debug("AUDIO RESPONSE", "Tried to write to closed FileStream, Knownbug"); return; } } }
public async Task HandleResponse(byte[] dataBytes) { var ar = AssistResponse.Parser.ParseFrom(dataBytes); //if (ar.DialogStateOut.MicrophoneMode == DialogStateOut.Types.MicrophoneMode.CloseMicrophone) //{ // return; //} if (ar.EventType == AssistResponse.Types.EventType.EndOfUtterance) { AudioRecorder.StopRecording(); if (!string.IsNullOrEmpty(ar.DialogStateOut?.SupplementalDisplayText)) { MainPage.SetLabelText(ar.DialogStateOut.SupplementalDisplayText); } if (string.IsNullOrEmpty(MainPage.ProgressPopup.GetText())) { MainPage.SetButtonImage("listen_blue.png"); } MainPage.ProgressPopup.Dismiss(); await Task.Run(Player.Prepare).ConfigureAwait(false); MainPage.SetActionButtonIsEnabled(true); } if (ar.SpeechResults?.Any(i => (int)i.Stability == 1) == false) { if (MainPage.Pref.GetRawVoiceRecognitionText()) { foreach (string transcript in ar.SpeechResults.Select(x => x.Transcript)) { MainPage.ProgressPopup.UpdateText(transcript); } } if (!MainPage.Pref.GetRawVoiceRecognitionText() && ar.SpeechResults.Any(i => i.Stability > 0.01)) { foreach (string transcript in ar.SpeechResults.Where(x => x.Stability > 0.01).Select(x => x.Transcript)) { MainPage.ProgressPopup.UpdateText(transcript); } } } if (!string.IsNullOrEmpty(ar.DialogStateOut?.SupplementalDisplayText)) { MainPage.SetLabelText(ar.DialogStateOut.SupplementalDisplayText); //return; } if (ar.ScreenOut != null) { MainPage.SetHtmlView(ar.ScreenOut.Data.ToStringUtf8()); } if ((ar.DialogStateOut?.VolumePercentage ?? 0) != 0) { var newVolumeLevel = Convert.ToInt32(15 * ar.DialogStateOut.VolumePercentage / 100); AudioManager.VolumeController.Level[AudioVolumeType.Media] = newVolumeLevel; MainPage.SetButtonImage("listen_blue.png"); MainPage.SetActionButtonIsEnabled(true); return; } if (ar.AudioOut?.AudioData.Length > 0) { await Player.WriteBuffer(ar.AudioOut.AudioData.ToByteArray()).ConfigureAwait(false); if (AudioPlayer.IsPrepared && !AudioPlayer.IsPlaying && Player.Buffered >= 1600) { AudioPlayer.IsPlaying = true; Player.Play(); } } }