DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. Using a Surface Dial to Scroll Through a Playing Track

Using a Surface Dial to Scroll Through a Playing Track

If you've been thinking of picking up the Surface Dial, check out how you can program it to do your bidding! At least when it comes to music.

Matt Lacey user avatar by
Matt Lacey
·
Nov. 25, 16 · Tutorial
Like (0)
Save
Tweet
Share
2.83K Views

Join the DZone community and get the full member experience.

Join For Free
So, I'm very impressed with the new Surface Dial. It's smaller than I was expecting but feels really nice to use. Plus it's easy to program!

Official links are here. Or see the sample I put together below:
  • Instructions on pairing.
  • Coding instructions.
  • Official sample.
Yes, it's an ugly app but I wanted to see how easy it would be to use it to skip back and forwards through a playing track. I'll often be listening to podcasts and skip back a little bit to have something repeated. I thought this would be a suitable tool for such a task.

It's a simple app.
  • Pick a track and it starts playing.
  • Tap the dial to toggle pause and playback.
  • Scroll the dial to move forwards and backward through the track.
  • There's also a button to toggle playback and a visualization of how far through the track it is.
Point of note:
  • It's possible to automatically select the (radial) menu item but I couldn't get it to set in the page constructor, OnNavigatedTo event, or the Loaded event. Because it's tied to the window (so different apps can have different menu items selected) I can understand why it needs a window available but I would have thought this possible in the Loaded event. Instead, I set this when a track is picked in my code.

There's lots more that can be done with the dial. Including:
  • Handling velocity of scrolling
  • Haptic feedback
  • Placement on screen (but I'll need a Surface Studio for that)
Code embedded below and here. 
<Page
    x:Class="MediaDial.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel Padding="50">
            <MediaElement x:Name="MediaElement" CurrentStateChanged="MediaElement_OnCurrentStateChanged" />
            <Button Click="PickTrackClicked">pick track</Button>
            <Button Click="PlayPauseClicked" Margin="0,10">Play/Pause</Button>
            <ProgressBar x:Name="Progress" Maximum="100" Minimum="0" Value="0" Height="20" />
        </StackPanel>
    </Grid>
</Page>


using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Media.Core;
using Windows.Media.Playback;
using Windows.Storage.Pickers;
using Windows.UI.Core;
using Windows.UI.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace MediaDial
{
    public sealed partial class MainPage : Page
    {
        private RadialController Controller;
        private RadialControllerMenuItem menuItem;

        public MainPage()
        {
            this.InitializeComponent();

            Controller = RadialController.CreateForCurrentView();
            Controller.RotationResolutionInDegrees = 1;

            Controller.RotationChanged += Controller_RotationChanged;
            Controller.ButtonClicked += Controller_ButtonClicked;

            this.menuItem = RadialControllerMenuItem.CreateFromKnownIcon("MediaDial", RadialControllerMenuKnownIcon.NextPreviousTrack);

            Controller.Menu.Items.Add(this.menuItem);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            Controller?.Menu.Items.Clear();
        }

        private void Controller_ButtonClicked(RadialController sender, RadialControllerButtonClickedEventArgs args)
        {
            TogglePlayback();
        }

        private void Controller_RotationChanged(RadialController sender, RadialControllerRotationChangedEventArgs args)
        {
            var delta = args.RotationDeltaInDegrees;

            if (delta > 0)
            {
                MediaElement.Position += TimeSpan.FromSeconds(1);
            }
            else
            {
                MediaElement.Position -= TimeSpan.FromSeconds(1);
            }

            if (MediaElement.CurrentState != MediaElementState.Playing)
            {
                SetPosition();
            }
        }

        private void PlayPauseClicked(object sender, RoutedEventArgs e)
        {
            TogglePlayback();
        }

        private void TogglePlayback()
        {
            if (MediaElement.CurrentState == MediaElementState.Playing)
            {
                this.MediaElement.Pause();
            }
            else
            {
                this.MediaElement.Play();
            }
        }

        private async void PickTrackClicked(object sender, RoutedEventArgs e)
        {
            var picker = new FileOpenPicker();
            picker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
            picker.FileTypeFilter.Add(".wmv");
            picker.FileTypeFilter.Add(".mp3");

            var file = await picker.PickSingleFileAsync();
            if (file != null)
            {
                var mediaSource = MediaSource.CreateFromStorageFile(file);
                var mediaPlaybackItem = new MediaPlaybackItem(mediaSource);

                MediaElement.SetPlaybackSource(mediaPlaybackItem);
            }

            Controller?.Menu.SelectMenuItem(this.menuItem);
        }

        private async void MediaElement_OnCurrentStateChanged(object sender, RoutedEventArgs e)
        {
            if (MediaElement.CurrentState == MediaElementState.Playing)
            {
                await Dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal,
                        async () => await RunTimer());
            }
            else
            {
                SetPosition();
            }
        }

        private async Task RunTimer()
        {
            while (MediaElement.CurrentState == MediaElementState.Playing)
            {
                SetPosition();

                await Task.Delay(1000);
            }
        }

        private void SetPosition()
        {
            try
            {
                Progress.Value = 100 / MediaElement.NaturalDuration.TimeSpan.TotalSeconds *
                         MediaElement.Position.TotalSeconds;
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc);
            }
        }
    }
}


And if you're interested, here are some other SurfaceDial projects to check out.

IT app Event Visualization (graphics) Podcast Velocity (JavaScript library) Plus (programming language) Coding (social sciences) Task (computing)

Published at DZone with permission of Matt Lacey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Building a REST API With AWS Gateway and Python
  • gRPC on the Client Side
  • Getting a Private SSL Certificate Free of Cost
  • Microservices 101: Transactional Outbox and Inbox

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: