Streaming videos to your Chromecast using Xamarin.Forms

Here’s a quick guide on how you can use a Xamarin.Forms app to stream videos to your Chromecast. Before we start, you should know that the only limitation to this is that the video needs a URL so we can’t use local videos. I’ll explain later on how you can solve this if you don’t already have your videos hosted somewhere.

Create the app

Create a new Xamarin.Forms project in Visual Studio. Use a blank template and add a button to your MainPage.xaml with a Clicked event handler.

Add NuGet package

Add the NuGet package SharpCaster to your shared class library. We’ll use this package to check what Chromecast devices are connected to our network and to load and play media to a specific device. The package doesn’t officially support .NET Standard 2.0, which is what the Xamarin.Forms shared class library targets, but it still works. There are other C# Chromecast libraries out there aswell that does support this, such as GoogleCast. Try this out if SharpCaster doesn’t do the trick.

List your Chromecast devices

Next we’ll search for the Chromecast devices connected to our network. For this to work it is important to note that your physical test device needs to be connected to the same network as your Chromecast devices. Simulators won’t do the trick. We will list the devices using the built-in DisplayActionSheet-method for Forms pages.

var chromecasts = await ChromecastService.Current.StartLocatingDevices();

var chromecastName = await DisplayActionSheet("Choose Chromecast", "Cancel", null, chromecasts.Select(x => x.FriendlyName).ToArray());

var selectedChromecast = chromecasts.FirstOrDefault(x => x.FriendlyName == chromecastName);

Select your video

As stated before, your video needs to be hosted somewhere for this to work. In one of my previous posts, I showed how you can use Azure Blob Storage to get a disposable URL for your video. For this example we’ll just be using the URL of a testing video provided by Google:

var videoUrl = "";

Cast it!

When you have your URL ready, we can finally cast the video to your preferred Chromecast device:

SharpCasterDemoController _controller = null;
ChromecastService.Current.ChromeCastClient.ConnectedChanged += async delegate { if (_controller == null) _controller = await ChromecastService.Current.ChromeCastClient.LaunchSharpCaster(); };
ChromecastService.Current.ChromeCastClient.ApplicationStarted +=
async delegate
    while (_controller == null)
        await Task.Delay(500);

    await _controller.LoadMedia(videoUrl, "video/mp4", null, "BUFFERED");

await ChromecastService.Current.ConnectToChromecast(selectedChromecast);

Hopefully the video should now turn up on your screen! You can read more about the controls you can implement on the GitHub page for SharpCaster.

I’ve provided a sample on GitHub with three different scenarios: one hard-coded with the test video URL, one for implementing your own hosting and one for prompting the user for a URL they want to cast. You can check it out here.

2 thoughts on “Streaming videos to your Chromecast using Xamarin.Forms”

  1. Hi, could you maybe update your app with the latest Sharpcast vesion? 1.04

    I don’t manage to get it to work.

    Many thanks

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.