/// <summary> /// Builds a question group for the given SRS entry. /// </summary> /// <param name="reference">Target SRS entry.</param> public SrsQuestionGroup(SrsEntry reference) { Reference = reference; Questions = new List <SrsQuestion>(); if (!string.IsNullOrWhiteSpace(reference.Meanings)) { Questions.Add(new SrsQuestion() { Question = SrsQuestionEnum.Meaning, ParentGroup = this }); } if (!string.IsNullOrWhiteSpace(reference.Readings)) { Questions.Add(new SrsQuestion() { Question = SrsQuestionEnum.Reading, ParentGroup = this }); } Audio = new VocabAudio(Reference); }
/// <summary> /// Builds a question group for the given SRS entry. /// </summary> /// <param name="reference">Target SRS entry.</param> public SrsQuestionGroup(SrsEntry reference) { Reference = reference; Questions = new List<SrsQuestion>(); if (!string.IsNullOrWhiteSpace(reference.Meanings)) { Questions.Add(new SrsQuestion() { Question = SrsQuestionEnum.Meaning, ParentGroup = this }); } if (!string.IsNullOrWhiteSpace(reference.Readings)) { Questions.Add(new SrsQuestion() { Question = SrsQuestionEnum.Reading, ParentGroup = this }); } Audio = new VocabAudio(Reference); }
/// <summary> /// Triggers when the media played successfully and ended. /// </summary> private void OnMediaEnded(object sender, EventArgs e) { if (_playingVocab != null) { _playingVocab.State = VocabAudioState.Playable; _playingVocab = null; } IsBusy = false; }
/// <summary> /// Triggers when the media failed to load or play. /// </summary> private void OnMediaFailed(object sender, ExceptionEventArgs e) { if (_playingVocab != null) { _playingVocab.State = VocabAudioState.Failed; _playingVocab = null; } IsBusy = false; }
/// <summary> /// Triggered when the media has been successfuly opened. /// </summary> private void OnMediaOpened(object sender, EventArgs e) { if (_player.NaturalDuration.TimeSpan.Ticks == UnavailableDurationTicks) { // Probably unavailable (or we're having terrible, terrible luck) if (_playingVocab != null) { _playingVocab.State = VocabAudioState.Unavailable; _playingVocab = null; } IsBusy = false; } else { // Audio is available. Play it! if (_playingVocab != null) { _playingVocab.State = VocabAudioState.Playing; } _player.Volume = Math.Min(1, Math.Max(0, Properties.Settings.Default.AudioVolume / 100f)); _player.Play(); _timeoutTimer.Stop(); } }
/// <summary> /// Builds and returns the audio URI for the given vocab. /// </summary> /// <param name="vocab">Vocab to use to build the URI.</param> /// <returns>URI built from the setting using the vocab.</returns> private Uri GetUri(VocabAudio vocab) { return new Uri(Kanji.Interface.Properties.Settings.Default.AudioUri .Replace("%kana%", vocab.KanaReading) .Replace("%kanji%", vocab.KanjiReading)); }
private void AsyncPlayVocabAudio(VocabAudio vocab) { if (vocab.State == VocabAudioState.Unavailable) { // Audio has already been found to be unavailable. // Do not play. IsBusy = false; return; } // Switch the state to loading. vocab.State = VocabAudioState.Loading; try { // If something was playing... if (_playingVocab != null) { // Switch it to playable state and stop. _playingVocab.State = VocabAudioState.Playable; _player.Stop(); // Note: this should not happen, but it's there just in case. } // Switch the playing vocab and open the audio file. _playingVocab = vocab; _timeoutTimer.Start(); _player.Open(GetUri(vocab)); } catch (Exception ex) { // Lots of things could go wrong. Let's just log the error and give up with a Failed state. LogHelper.GetLogger("AudioBusiness").Error("Could not open the audio.", ex); vocab.State = VocabAudioState.Failed; IsBusy = false; } }
/// <summary> /// Plays the vocab audio for the given vocab. /// </summary> /// <param name="vocab">Vocab to play.</param> public static void PlayVocabAudio(VocabAudio vocab) { if (!Instance.IsBusy && Instance.CheckUriSetting()) { Instance.IsBusy = true; DispatcherHelper.InvokeAsync(() => Instance.AsyncPlayVocabAudio(vocab)); } }
/// <summary> /// Event trigger. /// Triggered when the timeout timer ticks. /// Stops loading and returns the business to a non-busy state. /// </summary> private void OnTimeoutTick(object sender, EventArgs e) { if (IsBusy) { if (_playingVocab != null) { _playingVocab.State = VocabAudioState.Failed; _playingVocab = null; } _player.Stop(); IsBusy = false; } }
public ExtendedVocab(VocabEntity dbVocab, ExtendedSrsEntry srsEntry) { DbVocab = dbVocab; Audio = new VocabAudio(dbVocab); SrsEntry = srsEntry; }