Create a Google Chromecast app with Xamarin.Forms in under 10 minutes

While I was waiting in between builds, I decided to put on some radio in the background. I started looking for a good app on my desktop I found TuneIn, which is nice. But I couldn't Chromecast it to my Google Nest Mini device. I got the impression that TuneIn isn't much more than a aggregator of livestreams of radio stations around the world. I decided to take a few minutes, find the radio station live stream and roll my own macOS app. That app just takes the live stream and puts that on my Google Nest speaker. In this post, you'll learn how.

Just 10 minutes?! Come on, that's impossible!

I am not exaggerating, it was literally 10 minutes! Does it look pretty? Definitely not. Does it work? Absolutely!

In this post we will see how we can setup a very simple Xamarin.Forms application and add a NuGet library that will let us communicate with Google Chromecast devices. If you just can't wait, the code can be found here: https://github.com/jfversluis/CastRadio

And in 10 minutes, what will we achieve?

Let me present you with a screenshot, which you can see below.

Screenshot of the app UI running on macOS
CastRadio Chromecast application running on macOS

The interface is very simple, a StackLayout with inside an Entry, Button and ListView. The Entry is used to supply a URL to stream, the Button is to start casting and the ListView is to show the devices that are found on my network.

OK, but shouldn't we be using CollectionView now?

You certainly do! But it's not supported for macOS yet 🙁

I guess functionality counts, is at least the code beautiful?

Well... It depends! 😬 I have applied some MVVM principals, but since it was just a quick and dirty app there still needs to be a lot of error handling and other things. But actually, the code can mostly be captured in a few lines, you can see them below.

First things first, to implement the Cast protocol, I have used a package by kokone called GoogleCast.

From top to bottom. In the constructor you see I call the Init method. There I query my local network for devices and each one that I find I add to my ObservableCollection so it will be shown in my ListView.

On that same ListView, I have a data binding to the SelectedItem. This will make sure that whenever I select a device the backing property is filled. This is done by creating a two-way binding. For reference, have a look at the XAML below, which is the page that belongs to the code above. The data binding I am talking about happens on line 6.

I also have a Button in there. Whenever the button is clicked, it will take the selected device, connect to it, get a channel and feed it with the URL that you have entered above. In my case I've added my favorite local radio station stream.

And that's it! Now I hear my speaker bleeping and it starts to play the stream. Mission accomplished!

Anything else?

Not really! While using this daily I have noticed there are some things that might be nice to have, so maybe when I got another 10 minutes I'll play with it some more to see if I can implement things like volume control and make things a bit more robust and tidy.

Mostly I just wanted to show how powerful all of this stuff is and how easy to use. And because I have set this up in Xamarin.Forms I can also run this automatically on my iOS and Android devices or UWP, GTK and more if I would add them to this project.

Image by John Tekeridis via Pexels

14 thoughts on “Create a Google Chromecast app with Xamarin.Forms in under 10 minutes”

  1. Will it work when my app will be minimized? Do we not need foreground service? Or I’m missing something?

    • The way the Cast protocol works is mostly fire and forget. So when you have triggered the speaker to start playing, you can even close your app and it will continue playing 🙂

        • Same principle. You should almost see it as a REST API. You just send a url where the content is to the server and the server will take that content whether it’s video or audio and play it regardless of what happens to the requesting client. All other calls like setting the volume etc. Is the same thing. And at the same time you can request the current volume level etc.

          It’s not entirely the same concept, but it shows some similarities. Think about it; if you play something from, for example, Netflix on your TV from your phone you can walk out the door and the video will keep playing. I hope that makes it clear!

          • Exactly. But for one app I was using Vlc Player and I had to use foreground service, otherwise, android was stopping the background video after some times, say half an hour.

          • Hi Sir,
            Do you know any way to keep background work on in IOS?
            IOS is not allowing forground service logic like android.
            I’m trying on Xamarin form. Is there any tested library?

  2. Pointless app, just ask the Google home speaker to play whichever radio station you want to listen too

    • Last time I checked that didn’t work where I am. But even if it does, there are of course other scenarios you can think of to make this functionality useful

      • I understand the fun to code but yes, you can talk to your Google Mini 😉
        In the Netherlands just try: Hé Google, zoek radio twee. My mini quickly responds by saying ot will play via … tunein.

        But again: coding is probably more fun.

        • Coding is more fun always. But try it for Q-Music. And I don’t want to listen to the Belgian one… 😉

    • Not working where I am. At least not for the station I was going for. And besides it cost me 10 minutes, learned from it and hopefully other people now did too. What’s not to like?

  3. Or connect it via Bluetooth it takes 10 seconds 😂 but yeah I understand the engineering mindset 😜

Comments are closed.