Advertisement
Anheledir

SoundPreviewAction

Oct 2nd, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.64 KB | None | 0 0
  1. using System;
  2. using Windows.UI.Xaml;
  3. using Windows.UI.Xaml.Controls;
  4. using Windows.UI.Xaml.Controls.Primitives;
  5. using Microsoft.Xaml.Interactivity;
  6.  
  7. namespace Anheledir.Utilities
  8. {
  9.     public class SoundPreviewAction : DependencyObject, IAction
  10.     {
  11.         public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof (string), typeof (SoundPreviewAction), new PropertyMetadata(null));
  12.  
  13.         public static readonly DependencyProperty VolumeProperty = DependencyProperty.Register("Volume", typeof (double), typeof (SoundPreviewAction), new PropertyMetadata(0.5));
  14.  
  15.         public static readonly DependencyProperty PlayContentProperty = DependencyProperty.Register("PlayContent", typeof (object), typeof (SoundPreviewAction), new PropertyMetadata (new SymbolIcon(Symbol.Play)));
  16.  
  17.         public static readonly DependencyProperty StopContentProperty = DependencyProperty.Register("StopContent", typeof (object), typeof (SoundPreviewAction), new PropertyMetadata(new SymbolIcon(Symbol.Stop)));
  18.  
  19.         // 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
  20.         private Popup _helperElement;
  21.  
  22.         static SoundPreviewAction()
  23.         {
  24.         }
  25.  
  26.         private static MediaElement CurrentlyPlaying { get; set; }
  27.         private static Button CurrentlyPlayingButton { get; set; }
  28.  
  29.         /// <summary>
  30.         ///     This is the sound file that should be played on button click
  31.         /// </summary>
  32.         public string Source
  33.         {
  34.             get { return (string) GetValue(SourceProperty); }
  35.             set { SetValue(SourceProperty, value); }
  36.         }
  37.  
  38.         /// <summary>
  39.         ///     You can set the volume of the sound between 0.0 and 1.0, default is 0.5 (50%)
  40.         /// </summary>
  41.         public double Volume
  42.         {
  43.             get { return (double) GetValue(VolumeProperty); }
  44.             set { SetValue(VolumeProperty, value); }
  45.         }
  46.  
  47.         /// <summary>
  48.         ///     This content will be displayed while no sound is played, e.g. a Play-Button
  49.         /// </summary>
  50.         public object PlayContent
  51.         {
  52.             get { return GetValue(PlayContentProperty); }
  53.             set { SetValue(PlayContentProperty, value); }
  54.         }
  55.  
  56.         /// <summary>
  57.         ///     This content will be displayed while the sound is playing, e.g. a Stop-Button
  58.         /// </summary>
  59.         public object StopContent
  60.         {
  61.             get { return GetValue(StopContentProperty); }
  62.             set { SetValue(StopContentProperty, value); }
  63.         }
  64.  
  65.         public object Execute(object sender, object parameter)
  66.         {
  67.             if (string.IsNullOrWhiteSpace(Source))
  68.             {
  69.                 return false;
  70.             }
  71.  
  72.             Uri sourceUri;
  73.             if (!Uri.TryCreate(Source, UriKind.Absolute, out sourceUri))
  74.             {
  75.                 if (!Uri.TryCreate(string.Concat("ms-appx:///{0}", Source), UriKind.Absolute, out sourceUri))
  76.                 {
  77.                     return false;
  78.                 }
  79.             }
  80.  
  81.             if (CurrentlyPlaying != null)
  82.             {
  83.                 // There was already a sound playing, so stop it
  84.                 CurrentlyPlaying.Stop();
  85.  
  86.                 if (CurrentlyPlayingButton != null)
  87.                 {
  88.                     // We set the content of the previously playing sound-button back to the PlayContent
  89.                     CurrentlyPlayingButton.Content = PlayContent;
  90.                 }
  91.  
  92.                 if (CurrentlyPlaying.Source == sourceUri)
  93.                 {
  94.                     // In case we stopped the currently playing sound without starting another, we cancel execution at this point
  95.                     CurrentlyPlaying = null;
  96.                     return false;
  97.                 }
  98.             }
  99.  
  100.             _helperElement = new Popup();
  101.             CurrentlyPlaying = new MediaElement();
  102.             _helperElement.Child = CurrentlyPlaying;
  103.             CurrentlyPlaying.Visibility = Visibility.Collapsed;
  104.             CurrentlyPlaying.Source = sourceUri;
  105.             CurrentlyPlaying.Volume = Volume;
  106.             CurrentlyPlaying.MediaEnded += MediaElement_MediaEnded;
  107.             CurrentlyPlaying.MediaFailed += MediaElement_MediaFailed;
  108.             CurrentlyPlayingButton = sender as Button;
  109.             if (CurrentlyPlayingButton != null)
  110.             {
  111.                 CurrentlyPlayingButton.Content = StopContent;
  112.             }
  113.             _helperElement.IsOpen = true;
  114.             return true;
  115.         }
  116.  
  117.         private void MediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
  118.         {
  119.             if (CurrentlyPlayingButton != null)
  120.             {
  121.                 CurrentlyPlayingButton.Content = PlayContent;
  122.                 CurrentlyPlayingButton = null;
  123.                 CurrentlyPlaying = null;
  124.             }
  125.  
  126.             if (_helperElement != null)
  127.             {
  128.                 _helperElement.IsOpen = false;
  129.                 _helperElement.Child = null;
  130.                 _helperElement = null;
  131.             }
  132.         }
  133.  
  134.         private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
  135.         {
  136.             if (CurrentlyPlayingButton != null)
  137.             {
  138.                 CurrentlyPlayingButton.Content = PlayContent;
  139.                 CurrentlyPlayingButton = null;
  140.                 CurrentlyPlaying = null;
  141.             }
  142.  
  143.             if (_helperElement != null)
  144.             {
  145.                 _helperElement.IsOpen = false;
  146.                 _helperElement.Child = null;
  147.                 _helperElement = null;
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement