private async Task Record(TimingMessage message) { lock (this.keysInProcess) { this.keysInProcess["MICROPHONE"] = true; } await this.dispatcher.RunAsync( CoreDispatcherPriority.Normal, async () => { await this.audio.InitializeAudioRecording(); IRandomAccessStream stream; try { this.appSettings.IsListening = true; stream = await this.audio.CaptureAudio(new TimeSpan(0, 0, 0, 0, message.Ms)); } finally { this.appSettings.IsListening = false; } if (message.Autoplay.HasValue && message.Autoplay.Value) { this.player.AutoPlay = true; this.player.SetSource(stream, this.audio.GetFile().FileType); this.player.Play(); } // stores the image in Azure BLOB Storage var memStream = new MemoryStream(); var fileStream = stream.AsStreamForRead(); await fileStream.CopyToAsync(memStream); if (!await this.SendToDestinations(message, memStream, KnownFolders.VideosLibrary)) { await this.SendResult( new ResultMessage(message) { Result = this.audio.GetFile().Name, ResultId = 1 }); } else { if (message.Keep == null || !message.Keep.Value) { // remove original await fileStream.FlushAsync(); await this.audio.GetFile().DeleteAsync(); } } lock (this.keysInProcess) { this.keysInProcess["MICROPHONE"] = false; } }); }
private void Vibrate(TimingMessage timingMessage) { var vibrationDevice = VibrationDevice.GetDefault(); vibrationDevice.Vibrate(new TimeSpan(0, 0, 0, timingMessage.Ms / 1000, timingMessage.Ms % 1000)); }
private async Task Play(TimingMessage playMessage) { var folders = new Dictionary<string, StorageFolder> { { "MUSIC:", KnownFolders.MusicLibrary }, { "VIDEOS:", KnownFolders.VideosLibrary }, { "PICTURES:", KnownFolders.PicturesLibrary }, { "CAMERA:", KnownFolders.CameraRoll }, { "SAVED:", KnownFolders.SavedPictures } }; try { await this.dispatcher.RunAsync( CoreDispatcherPriority.Normal, async () => { // force start of whatever is most recently sent if (this.player.CurrentState != MediaElementState.Closed && this.player.CurrentState != MediaElementState.Stopped) { this.player.Stop(); } if (this.player.Source != null) { this.player.Source = null; } StorageFile file = null; var url = playMessage.Url; var isWebUrl = false; var colon = url.IndexOf(':'); if (colon > -1) { var root = url.Substring(0, colon + 1).ToUpperInvariant(); var folder = folders.SingleOrDefault(f => root.StartsWith(f.Key)); if (folder.Value != null) { file = await folder.Value.GetFileAsync(url.Substring(colon + 1)); } else { isWebUrl = true; } } if (isWebUrl) { this.player.Source = new Uri(url); } else { if (file == null) { await this.SendResult( new ResultMessage(playMessage) { ResultId = -3, Result = "file does not exist" }); return; } var stream = await file.OpenAsync(FileAccessMode.Read); this.player.SetSource(stream, file.ContentType); } this.player.Tag = playMessage; this.player.CurrentStateChanged += this.Player_CurrentStateChanged; this.player.Play(); }); } catch (Exception e) { await this.SendResult(new ResultMessage(playMessage) { ResultId = e.HResult, Result = e.Message }); } }