Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Controls.Primitives;
- using Microsoft.Xaml.Interactivity;
- namespace Anheledir.Utilities
- {
- public class SoundPreviewAction : DependencyObject, IAction
- {
- public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof (string), typeof (SoundPreviewAction), new PropertyMetadata(null));
- public static readonly DependencyProperty VolumeProperty = DependencyProperty.Register("Volume", typeof (double), typeof (SoundPreviewAction), new PropertyMetadata(0.5));
- public static readonly DependencyProperty PlayContentProperty = DependencyProperty.Register("PlayContent", typeof (object), typeof (SoundPreviewAction), new PropertyMetadata (new SymbolIcon(Symbol.Play)));
- public static readonly DependencyProperty StopContentProperty = DependencyProperty.Register("StopContent", typeof (object), typeof (SoundPreviewAction), new PropertyMetadata(new SymbolIcon(Symbol.Stop)));
- // The MediaElement will only be played when it is part of the visual tree, so we use a non-visible popup as a parent-element
- private Popup _helperElement;
- static SoundPreviewAction()
- {
- }
- private static MediaElement CurrentlyPlaying { get; set; }
- private static Button CurrentlyPlayingButton { get; set; }
- /// <summary>
- /// This is the sound file that should be played on button click
- /// </summary>
- public string Source
- {
- get { return (string) GetValue(SourceProperty); }
- set { SetValue(SourceProperty, value); }
- }
- /// <summary>
- /// You can set the volume of the sound between 0.0 and 1.0, default is 0.5 (50%)
- /// </summary>
- public double Volume
- {
- get { return (double) GetValue(VolumeProperty); }
- set { SetValue(VolumeProperty, value); }
- }
- /// <summary>
- /// This content will be displayed while no sound is played, e.g. a Play-Button
- /// </summary>
- public object PlayContent
- {
- get { return GetValue(PlayContentProperty); }
- set { SetValue(PlayContentProperty, value); }
- }
- /// <summary>
- /// This content will be displayed while the sound is playing, e.g. a Stop-Button
- /// </summary>
- public object StopContent
- {
- get { return GetValue(StopContentProperty); }
- set { SetValue(StopContentProperty, value); }
- }
- public object Execute(object sender, object parameter)
- {
- if (string.IsNullOrWhiteSpace(Source))
- {
- return false;
- }
- Uri sourceUri;
- if (!Uri.TryCreate(Source, UriKind.Absolute, out sourceUri))
- {
- if (!Uri.TryCreate(string.Concat("ms-appx:///{0}", Source), UriKind.Absolute, out sourceUri))
- {
- return false;
- }
- }
- if (CurrentlyPlaying != null)
- {
- // There was already a sound playing, so stop it
- CurrentlyPlaying.Stop();
- if (CurrentlyPlayingButton != null)
- {
- // We set the content of the previously playing sound-button back to the PlayContent
- CurrentlyPlayingButton.Content = PlayContent;
- }
- if (CurrentlyPlaying.Source == sourceUri)
- {
- // In case we stopped the currently playing sound without starting another, we cancel execution at this point
- CurrentlyPlaying = null;
- return false;
- }
- }
- _helperElement = new Popup();
- CurrentlyPlaying = new MediaElement();
- _helperElement.Child = CurrentlyPlaying;
- CurrentlyPlaying.Visibility = Visibility.Collapsed;
- CurrentlyPlaying.Source = sourceUri;
- CurrentlyPlaying.Volume = Volume;
- CurrentlyPlaying.MediaEnded += MediaElement_MediaEnded;
- CurrentlyPlaying.MediaFailed += MediaElement_MediaFailed;
- CurrentlyPlayingButton = sender as Button;
- if (CurrentlyPlayingButton != null)
- {
- CurrentlyPlayingButton.Content = StopContent;
- }
- _helperElement.IsOpen = true;
- return true;
- }
- private void MediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
- {
- if (CurrentlyPlayingButton != null)
- {
- CurrentlyPlayingButton.Content = PlayContent;
- CurrentlyPlayingButton = null;
- CurrentlyPlaying = null;
- }
- if (_helperElement != null)
- {
- _helperElement.IsOpen = false;
- _helperElement.Child = null;
- _helperElement = null;
- }
- }
- private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
- {
- if (CurrentlyPlayingButton != null)
- {
- CurrentlyPlayingButton.Content = PlayContent;
- CurrentlyPlayingButton = null;
- CurrentlyPlaying = null;
- }
- if (_helperElement != null)
- {
- _helperElement.IsOpen = false;
- _helperElement.Child = null;
- _helperElement = null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement