backgroundaudio

Background Audio in a Windows Store C# App

If you’ve been following my Twitter or you happen to know me, you might know that I’m developing an app for Windows 8/ Windows RT. I’ll leave the announcements for another day, but just know this: it’s gon’ be good.

One of the main problems I had when starting out on my app was when trying to play audio in the background – i.e. both when the app is running and when suspended. Googling for a complete guide was (and is) futile, so I’m going to share what I came up with in this post.

In WinRT, the way to play media is using the MediaElement. It’s a visual object, which means it needs to be instantiated in the XAML in order to work. This makes sense, given that the object is capable of playing both audio and video, but causes problems when navigating away from the page the MediaElement is on – for obvious reasons. So how can we use the MediaElement when we’re not on the page it was created on? There are a couple of workarounds out there, but essentially they’re the same thing. The answer to the question is: put the element on every page at the same time.

The simplest way to do this is to create an intermediary page between App.xaml and the first page we navigate to and stick the MediaElement in there. The cleanest (and my preferred) way is to use a style on the App.xaml’s Frame, as detailed by JimMan over on this Answers Thread:

We stick that XAML in our StandardStyles.xaml file (and merge it with LayoutRootStyle if it’s still around) – notice the AudioCategory attribute “BackgroundCapableMedia.” We then put this line in App.xaml.cs, just before we navigate to our first page:

Finally, we can play media in the codebehind of any page, using this:

If we try out the above, we find we can play media throughout our app. To enable actual background audio we have to tell Windows that our app can support it. All we need to do is edit our Package.appxmanifest file and declare we use background audio in the Declarations tab, like so:

So now we can play songs, audiobooks, podcasts and what have you both throughout our app and when our app is in the background (note: to test this, we need to use the suspend and resume debugging states in Visual Studio – the app isn’t actually in the background when we debug).

But WAIT! There’s more we have to do to make it maintainable from a development perspective. Don’t worry, it’s simple.

[Hopefully your app is built according to the MVVM pattern - i.e. your code isn't all in .xaml.cs files. If not, go fix that before continuing with the post, because it won't apply to your app.]

As mentioned earlier, the MediaElement is something which has to be present in the visual tree in order to work. We solved that by applying a style containing a MediaElement to our app’s root frame, but there’s another problem we didn’t come across: how can we control the media from a page which has no visual tree – like a model or viewmodel? We might need to do this if we want our users to be able to use their play/ pause button or play a playlist without requiring their input.

Going back to this code:

We’ll find that it won’t work in a viewmodel. A solution to this is keeping the MediaElement in a static field in your App.xaml.cs class -

- having all of your MediaElement event handlers as static methods in the same file, like so -

- and setting up the static MediaElement – but from our first page  (i.e. the first we navigate to).

That should be all we need to play, pause, skip, shuffle and everything else we could possibly want to do in the background! Any questions or opinions you can leave in the comment section below.

P.S. a quick note on these lines:

This runs the code  Player.Play();  on the UI thread. The reason this is necessary is again down to the MediaElement needing to be in the visual tree – so be wary of any heavy code you put in this lambda as it can easily gum up your UI if you’re not careful.

P.P.S. and keep an eye out for my app, from which the above code samples were taken, coming out in the next month or so. It really is gon’ be good.

Congratulations on your successful surgery! Now that you look like Michael Cera, you're one step closer to Hollywood stardom. I know you've always wanted that.

Class III Malocclusion

Taking a quick break from the technical posts

One of my good friends recently had surgery to correct his underbite (his top jaw closed behind his bottom jaw). He’s also somewhat of a blogger (using Blogger, though I try not to hold that against him), so he wrote a series of blog posts about his “class III malocclusion on a skeletal III base,” and the process of getting it sorted out. From the perspective of someone with only minor dental complaints (like me), it’s a fascinating read. Continue reading

cover

Ringtones for Windows Phone

I’ve used these four ringtones on my phones for a while now so I thought I’d share them. They’re tagged nicely and Windows Phone friendly (under 1MB, in mp3 and shorter than 40 seconds), so all you need to do is download them to your music library and then sync them to your phone. They’ll appear in Settings > Ringtones & Sounds and in the Alarms app.

Included are:

  1. Moon by Björk
  2. Garden by T.E.E.D. (a.k.a. the music from Nokia’s Lumia adverts)
  3. Mountain by Remember Remember
  4. Epicenter from SimCity 4 (which, btw, is still an awesome game)

I think they work pretty well as ringtones, so even if you don’t like the artists try ‘em out anyway!

Download from SkyDrive here

wp_mango_logo

Mango to Apollo (Updated)

The Verge broke a story today claiming that no current Windows Phone will be upgraded to Apollo come Autumn. This is what their source (who remains unnamed, by the way) said:

Now, a trusted source close to Microsoft tells us [the information in the evangelist video] is absolutely not the case, that instead there will be no upgrade path from Mango to Apollo.

Now this is obviously to be taken with a pinch of salt – as Windows Phone Daily puts it:

Let’s check the tally so far: MS Nerd says yes, Mary Jo Foley says no, a Microsoft evangelist says yesThe Verge says no, and MS Nerd said yes (again). What’s the answer? Your guess is as good as ours…and maybe that’s the point. It’s entirely possible Microsoft has been purposely sending out contradictory information in an effort to keep Apollo under wraps.

Let’s also remember the rumours that the kernel of Windows Phone 8 will be based on WinRT, and not WinCE as it currently is. In this light, I’d like to point out:

You know, all they could mean by “no upgrade path” is “no direct upgrade path” – in that one must flash their phone in order to upgrade. This would be similar to what some devs did when testing out Mango.

This would make sense if the rumours about changing the kernel were true.

It would also imply that a full backup suite is coming. Which would be awesome.

Final thought: Microsoft would be insane to not have some form of upgrade for their existing devices.

Microsoft, Nokia: stop letting rumours control your products’ marketing. It’s bad for everyone. And, The Verge: please stop being so Guardian in your reporting, you’re starting to gain a bad reputation.

Update: welp, that was hilariously wrong. Oh well.

Graph: Future importance of gaming products vs their targeted demographics

Shorts: On Gaming

‘Shorts’ are short posts – snippets of conversations and ideas.

Over on the forum I run, I’ve gained a minor reputation for being a “Microsoft PR guy.” Which I guess is partly true, but not from any inherent fanboyism – I’m just invested enough in their products to 1) know what they’re doing and 2) care about their future development.

Anyway, I decided to make a post outlining what I think Microsoft will do over the next couple of years with regards to gaming, and how other companies will be affected by it. Here’s the link to the original post. Continue reading

shortsonmetro1

Shorts: On Metro

‘Shorts’ are short posts – snippets of conversations and ideas. You might see them remade in the future, who knows…

So I saw this post in my Facebook feed…

…and it piqued my curiosity. How many people would be complaining about Metro?

Surprisingly, not that many. Maybe people are getting used to the idea. However, this comment did strike me:

Warnie L. Pritchett A philosophy? That’s a stretch. It’s just a UI, nothing else. And, as far as UIs go, it’s pretty garish, even WITH dynamic (live) tiles. We’ve come a long way (in large part thanks to Microsoft *and* Apple) from having big colored blocks as our UI. Metro feels like five steps backwards. It’s fine on Windows Phone, but on larger screens? No thanks – the blocks are just wasted space

To which, I replied:

Metro done well is fantastic – natural, fluid and pretty to boot – but done poorly it’s atrocious. There’s no such thing as a mediocre metro-style app – which is why so many people dislike it. There aren’t enough apps done right in the W8 CP. “People” and “Music” are pretty bad. “Weather” and “Mail” are brilliant.

@Warnie – it IS a philosophy. Windows 8 is not metro, it’s an implementation of metro – and this is the important point. Metro is all about getting rid of unnecessary chrome and standardising information display (hence the widgets all being the same size, apps being generally of the same format) WHILST being pretty to look at.

If you think about it, it’s the first step (on the UI side at least) towards the commoditisation of computing, as envisioned in Star Trek and similar sci-fi. Everything behaves the same way, hence people need to know less about how their software works.

Whether you find the implementations shown in the last few years (W8, WP, Zune (to a lesser extent)) to your taste or not is irrelevant. Microsoft creating “Metro” is simply a sign of the times – and don’t for one second think that Apple and Google aren’t implementing similar ideas. The future might not be Metro, but Metro and all of its ideas are definitely part of the future.

Indies are too Indie

As a student working from my bedroom, I surely qualify as an ‘indie’ developer. If there’s one thing I’ve struggled with over the last couple of months of developing (or attempting to develop) apps is a certain lack of support. I don’t feel like I’m part of a community – there are extremely nice people out there for sure, but I don’t feel a sense of ‘we’ (if I’m qualified to use that term). Continue reading

dustopensourced

I’m redecorating

I made a tweet recently talking about how I’ve learnt so much re: web design and design in general over the last couple of years. If you happen to have been following the various incarnations of this site over that time period, you’ll know that it’s looked pretty much the same throughout (dark with splashes of colour). I think it’s time to redecorate. Continue reading

getglue

My First Windows Phone App

Most of today was spent planning out my first app for Windows Phone. I’m making a client for GetGlue, a service which lets you ‘check in’ to TV, films, books and some other stuff too. I’ve not got much to show right now, but one thing I can say is that OAuth is extremely confusing for a first-time user. :’(

I hope to have a basic version working and submitted to the marketplace by the end of the week. I’ll update as things progress.

Update: They didn’t!