/// <summary> /// Creates a new instance of an <see cref="AudioPlayer"/> /// </summary> /// <param name="autoPlay">Whether to autoplay media.</param> /// <param name="defaultVolume">Default volume to set. Min-0, Max=1</param> /// <param name="tickIntervalMs">After how many milliseconds to update/call OnPlaying event.</param> public AudioPlayer(bool autoPlay = false, double defaultVolume = 1, double tickIntervalMs = 100) { _autoPlay = autoPlay; _volume = defaultVolume; Timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(tickIntervalMs) }; Timer.Tick += Timer_Tick; MediaPlayer = new MediaPlayer(); MediaPlayer.PlaybackSession.PlaybackStateChanged += (s, e) => OnStateChanged?.Invoke(s); MediaPlayer.MediaEnded += (s, e) => OnMediaEnded?.Invoke(); MediaPlayer.Volume = defaultVolume; MediaPlayer.AutoPlay = autoPlay; MediaPlayer.AudioCategory = MediaPlayerAudioCategory.Media; MediaPlayer.MediaFailed += (s, e) => OnError?.Invoke(e); MediaPlayer.MediaOpened += (s, e) => OnOpened?.Invoke(s.PlaybackSession); MediaPlayer.PlaybackSession.BufferingProgressChanged += (s, e) => OnBuffer?.Invoke(s.BufferingProgress); MediaPlayer.PlaybackSession.DownloadProgressChanged += (s, e) => OnDownload?.Invoke(s.DownloadProgress); }
private void MeElement_MediaEnded(object sender, RoutedEventArgs e) { this.Close(); OnMediaEnded?.Invoke(this, new EventArgs()); }