public void Seek(TimeSpan position) { if (subInputNode != null) { throw new InvalidOperationException("Can't Seek while Fading"); } if (mainInputNode != null) { mainInputNode.Seek(position); } }
private void Progress_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { if (_fileInputNode == null) { return; } if (Math.Abs(_fileInputNode.Position.TotalSeconds - e.NewValue) < 0.5) { return; } _fileInputNode.Seek(TimeSpan.FromSeconds(e.NewValue)); }
private void Loop_Click(object sender, RoutedEventArgs e) { if (fileInput != null) { // If turning on looping, make sure the file hasn't finished playback yet if (fileInput.Position >= fileInput.Duration) { // If finished playback, seek back to the start time we set fileInput.Seek(fileInput.StartTime.Value); } fileInput.LoopCount = null; // infinite looping } }
private void OnFileCompleted(AudioFileInputNode sender, object args) { Execute.BeginOnUIThread(() => { _graph.Stop(); _timer.Stop(); _fileInputNode.Seek(TimeSpan.Zero); _state = PlaybackState.Paused; UpdateGlyph(); _displayRequest.RequestRelease(); DurationLabel.Text = _fileInputNode.Duration.ToString("mm\\:ss"); Slide.Value = 0; }); }
private void LoopToggle_Toggled(object sender, RoutedEventArgs e) { // Set loop count to null for infinite looping // Set loop count to 0 to stop looping after current iteration // Set loop count to non-zero value for finite looping if (loopToggle.IsOn) { // If turning on looping, make sure the file hasn't finished playback yet if (fileInput.Position >= fileInput.Duration) { // If finished playback, seek back to the start time we set fileInput.Seek(fileInput.StartTime.Value); } fileInput.LoopCount = null; // infinite looping } else { fileInput.LoopCount = 0; // stop looping } }
// Async Speak output of a text private async Task PlayAsyncLow(SoundBite soundBite) { _playing = true; // locks additional calls for Speak until finished talking this bit if (!_canPlay || _audioGraph == null || _deviceOutputNode == null) { LOG.LogError($"PlayAsyncLow: Some items do not exist: cannot play..\n [{_audioGraph}] [{_deviceOutputNode}]"); await EndOfSound( ); return; } // don't reload if the sound is already in use if (soundBite.Melody != _soundInUse?.Melody) { // if a prev. Node exists, remove it if (_fileInputNode != null) { _audioGraph.Stop( ); _fileInputNode.FileCompleted -= _fileInputNode_FileCompleted; _fileInputNode.RemoveOutgoingConnection(_deviceOutputNode); _fileInputNode.Dispose( ); _fileInputNode = null; } // set new sound _sound = _installedSounds.Where(x => x.Melody == soundBite.Melody).FirstOrDefault( ); if (_sound == null) { LOG.LogError($"PlayAsyncLow: Melody has no Audiofile: {soundBite.Melody} - cannot play"); await EndOfSound( ); return; } StorageFile file = await StorageFile.CreateStreamedFileAsync($"{_sound.Id}.{_sound.SType}", StreamedFileWriter, null); // create the InputNode var resultAF = await _audioGraph.CreateFileInputNodeAsync(file); if (resultAF.Status != AudioFileNodeCreationStatus.Success) { LOG.LogError($"PlayAsyncLow: AudioFileNodeCreationStatus creation: {resultAF.Status}" + $"\nExtError: {resultAF.ExtendedError}"); await EndOfSound( ); return; } _fileInputNode = resultAF.FileInputNode; _fileInputNode.FileCompleted += _fileInputNode_FileCompleted; _fileInputNode.AddOutgoingConnection(_deviceOutputNode); _audioGraph.Start( ); _soundInUse = _sound.AsCopy( ); } // we capture problems through Exceptions here - the settings and restrictions seem not complete in the Docs try { // Play it // cannot start after prev end - so set it null and seek to start of file _fileInputNode.StartTime = null; _fileInputNode.EndTime = null; // cannot start after prev end - so set it null _fileInputNode.Seek(new TimeSpan(0)); // have to seek to Start, we cannot assign a StartTime before the current Position // only now we can set any new start and end... (that is not in the documents...) _fileInputNode.StartTime = TimeSpan.FromSeconds(soundBite.Tone * _soundInUse.ToneStep_sec); _fileInputNode.EndTime = _fileInputNode.StartTime + TimeSpan.FromSeconds((soundBite.Duration < 0) ? _soundInUse.ToneDuration_sec : soundBite.Duration); _fileInputNode.OutgoingGain = soundBite.Volume; _fileInputNode.LoopCount = (int)soundBite.Loops; // counts down in the Completed Callback - track completeness there (not in docu...) _fileInputNode.PlaybackSpeedFactor = soundBite.SpeedFact; // Plays in the current Thread - cannot be waited for in the same thread _fileInputNode.Start( ); //_audioGraph.Start( ); } catch (Exception e) { LOG.LogError($"PlayAsyncLow: Sample Setup caused an Exception\n{e.Message}"); await EndOfSound( ); } }
/// <summary> /// Loads audio file. /// </summary> /// <param name="PlayPosition"></param> /// <returns></returns> private async Task LoadAudioFile(TimeSpan PlayPosition) { if (soundFile != null) { ClearErrorMessage(); StartButtonEnabled = false; StopButtonEnabled = false; FileName = ""; try { await BuildAudioGraph(); } catch (Exception ex) { ShowErrorMessage(ex.Message); return; } emitter = new AudioNodeEmitter(AudioNodeEmitterShape.CreateOmnidirectional(), AudioNodeEmitterDecayModel.CreateCustom(0.1, 1), AudioNodeEmitterSettings.None); emitter.Position = new Vector3((float)X, (float)Z, (float)-Y); CreateAudioFileInputNodeResult inCreateResult = await graph.CreateFileInputNodeAsync(soundFile, emitter); switch (inCreateResult.Status) { case AudioFileNodeCreationStatus.FormatNotSupported: case AudioFileNodeCreationStatus.InvalidFileType: ShowErrorMessage(String.Format("Could not load {0}. \n\nPlease choose a different file - For Windows Spatial Sound processing, audio files must be Mono 48kHz PCM Wav.", soundFile.Name)); return; case AudioFileNodeCreationStatus.UnknownFailure: case AudioFileNodeCreationStatus.FileNotFound: ShowErrorMessage(String.Format("Audio file load error: {0}", inCreateResult.Status.ToString())); return; } //Dispose of previous input node. if (currentAudioFileInputNode != null) { try { currentAudioFileInputNode.Dispose(); } catch (ObjectDisposedException) { //Do nothing } } currentAudioFileInputNode = inCreateResult.FileInputNode; currentAudioFileInputNode.AddOutgoingConnection(deviceOutput); currentAudioFileInputNode.FileCompleted += CurrentAudioFileInputNode_FileCompleted; FileName = currentAudioFileInputNode.SourceFile.Name; if (PlayPosition != TimeSpan.Zero) { currentAudioFileInputNode.Seek(PlayPosition); graph.Start(); StopButtonEnabled = true; } else { StartButtonEnabled = true; } } }