I thought I’d do something foolish today and decided to play with a Windows Phone API I never used before – the VibrateController. How about playing back a rhythm? I coded up a very simple API to play back sequences of vibrations. You just build a VibrationSequence object with a list of Vibrations that specify vibration duration and a delay between the vibration start and the next vibration.
This is the code that plays it back:
var seq = new VibrationSequence( new Vibration(250, 500), new Vibration(250, 1250), new Vibration(200, 250), new Vibration(250, 500), new Vibration(250, 1500)); Play(seq);
And here is the VibrationSequence API:
namespace VibrationPlayground { using System; /// <summary> /// The vibration. /// </summary> public class Vibration { /// <summary> /// Gets the vibration duration. /// </summary> public TimeSpan Duration { get; private set; } /// <summary> /// Gets the total duration from the vibration start to the next <see cref="Vibration"/> object in <see cref="VibrationSequence"/>. /// </summary> public TimeSpan TotalDuration { get; private set; } /// <summary> /// Initializes a new instance of the <see cref="Vibration"/> class. /// </summary> /// <param name="durationMs"> /// The duration ms. /// </param> /// <param name="totalDurationMs"> /// The total duration ms. /// </param> public Vibration(double durationMs, double totalDurationMs) { this.Duration = TimeSpan.FromMilliseconds(durationMs); this.TotalDuration = TimeSpan.FromMilliseconds(totalDurationMs); } } }
namespace VibrationPlayground { using System; using System.Collections.ObjectModel; using System.Threading; using Microsoft.Devices; /// <summary> /// The vibration sequence. /// </summary> public class VibrationSequence { /// <summary> /// Gets the sequence of <see cref="Vibration"/> objects. /// </summary> public ReadOnlyCollection<Vibration> Vibrations { get; private set; } /// <summary> /// Initializes a new instance of the <see cref="VibrationSequence"/> class. /// </summary> /// <param name="vibrations"> /// The vibrations. /// </param> public VibrationSequence(params Vibration[] vibrations) { this.Vibrations = new ReadOnlyCollection<Vibration>(vibrations); } /// <summary> /// Starts playing the vibration sequence. /// </summary> /// <param name="onComplete"> /// The action to invoke when sequence playback is complete. /// </param> public void Start(Action onComplete) { ThreadPool.QueueUserWorkItem( s => { foreach (var vibration in Vibrations) { VibrateController.Default.Start(vibration.Duration); Thread.Sleep(vibration.TotalDuration); } onComplete.Invoke(); }); } } }
The project is available here. Does not seem to work on the emulator, or maybe my laptop lacks a vibrator?
[…] Good Vibrations on Windows Phone Share this:FacebookTwitterRedditDiggStumbleUponLinkedInTumblrEmailPrintLike this:LikeBe the first to like this post. […]
I don’t see where you are getting Play() from…
It’s there in the code download: private static void Play(VibrationSequence seq){seq.Start(() => Play(seq));}
Thanks for the quick reply! Now I can get my app submitted much faster! Thanks!