Tag Archives: Windows Phone

Tips and tricks when using voice recognition in a Windows Phone 8 app

While working (again) on 2Day, I encountered small issues with voice recognition. The next version of 2Day will bring support for both speech recognition (use your voice to set the title of a task) and voice commands (speak to your phone to perform operations, for example: “2Day add a reminder tomorrow at 9PM”).

While developing those new features I used two distinct API for voice recognition:

The first one provides no UI while the second one includes a default GUI which is uniform across apps and utilities:

Listenning

Issue 1: stop music playback automatically

In 2Day, I use the SpeechRecognizerUI when the user must confirm a choice while seeing what is visible on the screen. When using those APIs I faced a first problem which is music playback is not stopped automatically during voice recognition. It turns out a simple API can be used for this:

public void PauseMusicPlayback()
{
    this.wasPlayingMusic = MediaPlayer.State == MediaState.Playing;
    if (this.wasPlayingMusic)
    {
        FrameworkDispatcher.Update();
        MediaPlayer.Pause();
        FrameworkDispatcher.Update();
    }            
}

public void ResumeMusicPlayback()
{
    if (this.wasPlayingMusic)
    {
        // it looks like FrameworkDispatcher.Update() is needed when using the MediaPlayer
        FrameworkDispatcher.Update();
        MediaPlayer.Resume();
        FrameworkDispatcher.Update();
    }
}

With a simple boolean field, I can save before starting voice recognition if I will have to resume music playback.

Issue 2: use default sound with SpeechRecognizer

When using the SpeechRecognizer class (the one without the default UI) I faced another problems: start/end of voice recognition is not surrounded by the small system sound that indicates that the recognition has started/ended. I thought the easiest way to get this sound as a WAV file on my machine was to use an Audio Capturing tool such as Audacity.

Long story short: that was a bad idea. And it turns out… The original sound files are installed with the SDK !

If you browse the following directory C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Sounds you will be able to get the 2 WAV files ! And use them in your app (you can see my older blog post about how to play WAV file in a Silverlight Windows Phone app).

SoundFiles

Hope it helps 🙂

Wanted: Windows Phone / Windows 8 developers for 2Day !

Almost one year ago, I decided to create a simple but efficient todo list application for Windows Phone 7. In April 2012, I released 2Day, a fast and fluid todo list application. Today, while I’m busy preparing a new version which leverage new stuff available in Windows Phone 8 I also have big plans for Windows 8.

And now, that just too much for a single man. This is why, I’m now actively looking for a couple of developers who would like to join me in the 2Day development team!

If you:

  • Have development experience in C# and XAML (WPF, Silverlight, Windows Phone or Windows 8)
  • Want to work on an interesting app full of innovative features
  • Speak English or French
You will enjoy:
  • the latest cool technologies available
  • working in a international context (2Day is localized in 5 languages)
  • engaging directly with 2Day’s users through the UserVoice website
  • organize you work with the team
  • build the Windows 8 app from the ground up

Note: I’m not offering full time job here. 2Day is a personal development project I’m doing on my free time 😉

Please drop me an email using the Contact form or via Twitter. I Hope to get in touch with you soon.

Hello World, WinRT !

Now that the mysterious WP7 app I was working on for the last few months is done (at least v1.0!) and is in the certification pipeline, I finally have some time to start playing with WinRT.

Of course, the first thing I’m playing with is the port of this WP7 app to WinRT. In this post, I’m sharing the aspects I discovered this afternoon while trying to port one of the project from Windows Phone to WinRT. Let’s be clear my goal was just to see what it’s look like to port the code, I wasn’t able of course to port the WP7 app to WinRT in an afternoon…

Tooling

The development environment for Windows 8 is currently:

  • Windows 8 Consumer Preview
  • Visual Studio 11 Beta
  • Blend for Visual Studio Beta

Because I wanted to have the Windows Phone environment too on this machine, I also installed:

The WP7 app currently uses various libraries (all of them are described in this blog post), today I just downloaded the Windows 8 version of MVVM Light (thanks Laurent).

Hello, World

As a developer, one of the first thing we try when we target a new platform is to display some kind of message on the screen. So I first tried the famous MessageBox.Show() but I quickly realized the API has changed… If you want to display a message box, you must use the MessageDialog class:

MessageDialog dialog = new MessageDialog("Content", "Title");
dialog.ShowAsync();
// execution continues here immediately after showing the dialog

As you can see, displaying the dialog is actually an asynchronous operation. If we want to perform an action when the dialog closed, we must await that call:

MessageDialog dialog = new MessageDialog("Content", "Title");
await dialog.ShowAsync();
// execution continues here when the dialog closes

If you want to display additional buttons, you can add commands to the dialog:

MessageDialog dialog = new MessageDialog("Content", "Title");
dialog.Commands.Add(new UICommand { Label = "test" });
IUICommand result = await dialog.ShowAsync();

Namespace changes

Many namespace has changed between Windows Phone and WinRT, you can use pre-processor directives like the following

#if WINDOWS_PHONE
using System.Windows.Data;
#else
using Windows.UI.Xaml.Data;
#endif

IValueConverter

Another thing I noticed is the fact that the IValueConverter interface has changed (notice the last parameter…)

WPF/Silverlight/WindowsPhone version (documentation):

public interface IValueConverter
{
    object Convert(object value, Type targetType, object parameter, CultureInfo culture);
    object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
}

WinRT version (documentation):

public interface IValueConverter
{
    object Convert(object value, Type targetType, object parameter, string language);
    object ConvertBack(object value, Type targetType, object parameter, string language);
}

Storage

The storage story in WinRT can be summarized by the following:

  • file system access is heavily restricted (this was already the case for Silverlight and Windows Phone)
  • most storage API are asynchronous

Here is an example taken from the WinRT documentation:

// write a file
async void WriteTimestamp()
{
    StorageFolder localFolder = ApplicationData.Current.LocalFolder;

    StorageFile sampleFile = await localFolder.CreateFileAsync("dataFile.txt", CreationCollisionOption.OpenIfExists);
    await FileIO.WriteTextAsync(sampleFile, DateTime.Now.ToString());
}

// read a file
async Task ReadTimestamp()
{
    try
    {
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;

        StorageFile sampleFile = await localFolder.GetFileAsync("dataFile.txt");
        String timestamp = await FileIO.ReadTextAsync(sampleFile);
        // Data is contained in timestamp
    }
    catch (Exception)
    {
        // Timestamp not found
    }
}

Resources

The WP7 app contains several resource files (.resx). The resources are available in the XAML (using a binding to a static resource) and in C# using the static types that are automatically generated with the resx file. Well, it looks like those static types do not exist anymore in WinRT (also, the extension of the resource file has changed to .resw).

The resources can now be accessed using the ResourceLoader type:

res = new ResourceLoader("PrototypeZero/AppResources");
string value = res.GetString("StringKey");

Conclusion

I think .Net developers should not be afraid by WinRT. If you already start playing with C# Async you will be familiar with the new asynchronous API. As far as I’ve gone with the comparison I think we can reasonably imagine having a good code base shared between WinRT and Windows Phone. I can safely continue to use the XAML knowledge I got about 5 years ago on WinRT 🙂

Of course there are many other things we can compare between WinRT and Windows Phone… This first post is just the beginning 🙂