Xamarin Android Player For Mac

  

By Doumer

  • Uncategorized ,
  • 16 May

Jun 22, 2018 By the end of this article, you will have a working Xamarin.Android installation integrated into Visual Studio for Mac, and you'll be ready to start building your first Xamarin.Android application. Android SDK Setup. Visual Studio includes an Android SDK Manager that replaces Google's standalone Android SDK Manager.

Sep 19, 2021 Xamarin Android Player helps simulate, debug, demo or run Android apps in a fast and hassle-free environment. Xamarin Android Player has deep integration with Visual Studio and Xamarin Studio and a native user interface on both Mac & Windows. Running Android on an x86 virtual machine using hardware accelerated virtualization and OpenGL, Xamarin. A light-weight and easy to use cross-platform audio player for Windows UWP/WPF, Xamarin.iOS, Xamarin.Android, Xamarin.Mac, Xamarin.tvOS, Tizen and Xamarin.Forms. Load wav and mp3 files from any location including a shared library. Works well for sound effects or music. Multiple instances can be instantiated to play multiple sources simultaniously.

Hi friends welcome back for another post. Today we will talk about playing media in our Xamarin Forms apps. We won’t be using Xamarin Community Toolkit’s media element. Instead, we will use a VLC media player in Xamarin Forms. Why? because VLC media player can be an awesome alternative to the native media players available in Android and iOS. These native players work well, but are limited and lack some important functionalities.

What is VLC Media Player

VLC is a popular free open-source media player that is available on every major OS. It originated as a student project at l’ecole central de Paris. This project named VideoLan was initially meant to consist of a client and a server, to stream videos across the school’s campus. VLC which stands for VideoLan Client was initially the client part of the project. Now it is mostly known for its powerful media player functionalities. Source: Wikipedia.

  • Xamarin Android Player for MAC. This is the other best emulator to run your favorite android apps in your MAC OS. The setup process is a little bit lengthy. But you just need to proceed with the instructions given by it. And with the help of this emulator, you can get your favorite apps run on your MAC PC.
  • Xamarin Android Player 0.6.5 on 32-bit and 64-bit PCs. This download is licensed as freeware for the Windows (32-bit and 64-bit) operating system on a laptop or desktop PC from programming software without restrictions. Xamarin Android Player 0.6.5 is available to all software users as a free download for Windows.

Why Use VLC Media Player in Xamarin Forms

You might ask yourself; why do we need another media player, when Android and iOS have their native media players. The answer is simple, the native media players on each of these platforms are not powerful enough to handle certain scenarios like HLs streaming, Some file formats, and protocols… The VLC media player can be a great solution to implement these features in your app.

What we will Cover Today

  • Adding VLC Media Player to your Xamarin Forms project
  • Playing media
  • Important things to consider when using VLC Media Player
  • Controlling media with VLC media player
  • Streaming media from sources that require authentication
  • The Media Player Element
  • A brief note about playlist management

Adding VLC Media Player in Xamarin Forms Projects

It is very easy to get started with VLC media player. You first have to add appropriate nuget packages into your projects.

  • Every Project (Platforms + shared project):
    • VideoLAN.LibVLC.Forms
  • iOS Project
    • VideoLAN.LibVLC.iOS
  • Android Project
    • VideoLAN.LibVLC.Android

Playing media

In this demo, we will play video, and this is done just as you would do if you want to play audio. First, on my main page, in XAML, I added the video view.

This video view will display the video we are playing. The next thing we have to do is, create a media player and pass the instance to the video view. Then we call the “Play” method of the media player. This plays the media we passed in the URL. This is very basic, the next thing is to control the media we are playing.

NOTE: If you want to play music, you need only to create the media player as we did above, and call its “Play” method. This plays media in your app, even when the app is in the background.

Important things to consider when using VLC Media Player

After using VLC library, and talking with the guys maintaining this awesome library I learned from them some important things to take into consideration when using VLC media player.

  • After streaming your media, you should consider handling the disposal of the libvlcsharp objects (libvlc, media player, media)
  • When playing multiple media items with VLC, it is better to use one libvlc instance only and multiple media players
Xamarin

Controlling Media

Now we know that playing media with VLC Media Player in Xamarin Forms is easy. The next thing is to let our users control the media just like any awesome media-playing app we know. For this, I added a set of buttons, sliders on our main page. The buttons will serve for (Play, Pause, Forward…) the sliders will be for volume and seek. We will go through each of these functionalities one by one. In the code behind our main page, I put the event handler methods for each of these actions performed on the UI.

Play, Pause and Stop

The most basic functionality a media-playing app should have is to play/pause media. We have above the “Play” method this method lets us play a media item passed to the media player. This is fine if we are playing media for the first time, thus we want to start playing from the beginning of the media. But what if we want to play, pause when the user wants, and resume later? This is easy, as we can see below.

Now, once we are done playing, what if we want to stop the media? Easy… check how it is done below.

Move Forward, Backward and Seek-to Specific Time

Now, our user doesn’t want to watch a specific part of our movie, what does he do ? he moves the stream forward. What if he changes his mind and wants to go back? What if he wants to watch only a specific part of the movie and wants to seek the stream to that part? VLC Media Player got you covered. This is how we do it.

Note that the media player item has a “Timeproperty. We can set a value to this property that will correspond to the time position where we want it to stream.

Once we do that, we can use the above method to move forward or backward for 10 seconds in this example.

When a user moves the duration slider, it causes the slider’s value to change. Since our slider has a min value of 0 and a max value of 100, we can consider its value as a percentage played and that left to play. The VLC Media Player provides us the “Position” property that is the movie’s position percentage in a range of 0.0 to 1. Taking this into consideration, here is how we can leverage it to implement the seek functionality.

Displaying Elapsed time and Duration

Users want to know how long our movies, music will last and how much of it they have already watched. So we need to implement this functionality too. To do this, we will need to subscribe to a few media player events (PositionChangedand Playing). When the media player starts playing, the “Playing” event is called. When this event is fired, we can get the media’s duration from the “Length” property of VLC media player object.

As time passes and the user streams more of the movie, we want to display how long he has been watching. We do this by subscribing to the player’s “TimeChanged” event. In there, we get the media player’s “Time” property and display it to the user.

Controlling Volume

If our users want to mute our player or increase the volume for some reason, it is extremely easy. To mute, here is how it is done.

To control the volume, it is even easier.

Streaming Media From Sources that Require Authentication

Most often, you won’t stream media from free public sources. You will need the player to authenticate to that source. Unfortunately, VLC does not have a built in system for authentication, last time I checked, this was to be implemented. But, luckily we have a way around this! To authenticate, instead of passing headers to the player, we use an http client to query the API with the appropriate headers, then create a stream to the resource that we pass to the player as a “StreamMediaInput“. Note, if you are downloading media to play locally on iOS, here is an article demonstrating how to download this data from an API that streams its content bytes by bytes.

Error Handling

Once the media player starts playing media, errors might occur. If it is streaming, the network connection might be interrupted for example. We listen to errors that occur in the player with the “EncounteredError” event.

The Media Player Element

The VLC Media Player Element is a view that contains a video view and controls already available to manipulate any media stream at its disposal. This offers a plug-and-play functionality you can leverage to quickly build apps that consume media without the burden of writing UI elements yourself. You can customize this control to suit your purpose. Read more about this in this post by Martin Finkel, who introduces this awesome control.

  • This control derives from Xamarin Forms’ content view so, it behaves just as such. If its appearance is not very attractive to you (I’m sure it won’t be :-P) it can be modified and styled like any Xamarin forms control. Here is an example.
  • This control actually requires Fontawesome to be installed, since its image assets depend on fontawesome, ( Note: this dependency on fontawesome is temporary, as mentioned by Martin Finkel in the article above). Note:You have to add font awesome the traditional way, that is in each platform project. Doing this in the shared project as recommended for the latest Xamarin forms releases didn’t work. If it does for you, please leave a comment.

Adding the media player element to your project is extremely easy. First, add font-awesome as mentioned above and add an instance of the media player element to your page. Then bind its properties to the ViewModel. In our source code, we added a small sample demonstrating how to use this control. The sample is inspired by this awesome demo.

Xamarin

Playlist Management With VLC Media Player

One of the features several media playing apps have in common, is allowing users to read through multiple items. VLC bindings for Xamarin only have a tiny portion of VLC’s native libraries list management. This portion can’t be used to manage playlists, and bringing this native code to Xamarin bindings is not planned as mentioned here by one of the engineers maintaining this library. Meaning that you need to implement your own playlist management logic on top of VLC. But this is not a big deal. The VLC media player has an event called “EndReached” this event is fired once the media that is currently playing ends. You can listen to this event and play the next item, or select a random item from your playlist, etc. This allows you to build your own playlist logic.

Conclusion

Today we covered one of the most powerful media players out available out there for mobile devs. I’ve been a long-time user of VLC on Windows, Linux, and Mac. Therefore, working with this amazing library was such a pleasure. I was working on a project that required media playing functionality, but iOS’s native player didn’t do the job. Luckily, VLC came in to save the day. This article contained a condensed version of some of the things I learned throughout this journey.

Useful Links

Follow me on social media and stay updated

Xamarin is a cross-platform technology that makes it possible to build native applications for Android and iOS using a single, shared codebase. Like other technologies such as React Native and NativeScript, it allows development teams to spend less time writing code for both platforms.

Xamarin is open-source (and free). Under the hood, it uses Mono (a version of the Microsoft .NET runtime), so Xamarin apps are usually written in C#. You can build Xamarin apps on Windows using Visual Studio, or Mac using Visual Studio for Mac. Even though Xamarin apps are not written in Swift (or Java), they still look and feel like real, native apps on the device.

In this tutorial, I’ll show you how to use Xamarin to build a basic app for both iOS and Android, even if you’ve never done any app development before!

Set Up Xamarin on Windows

Skip to the next section if you have a Mac!

If you don’t have Visual Studio installed, download the free Community Edition from Microsoft.

When you install Visual Studio, be sure to pick the Mobile development with .NET workload, which installs the Xamarin tools you need:

If Visual Studio is already installed, open the Visual Studio Installer to make sure you have the above workload selected.

Xamarin android player free download

Once you have these tools installed, you’re ready to create a Xamarin project! Skip the next section.

Set Up Xamarin on Mac

If you have a Mac, you’ll need to install Visual Studio for Mac. Follow the official instructions to install the free Community Edition.

Once the application is installed, you’re ready to create a Xamarin project!

Xamarin and Xamarin Forms

The base Xamarin SDK contains API bindings for each mobile platform, so you can call Android or iOS APIs from C# code. This allows you to build native apps using shared C# code, but you still need to design the UI separately for each platform.

Xamarin.Forms is an additional library that makes it possible to build your UI once (in XAML, a markup language for describing UI layouts). Xamarin.Forms then does the hard work of translating your XAML layout into the appropriate UI elements on the target platform. You can drop down to the “lower” Xamarin SDK level and interact with the platform APIs whenever you need to.

Deciding whether to use Xamarin.Forms in your project depends on how complex your app is. If you’re building an app that needs UI ultra-tailored for each platform or includes a lot of complex user interactions (such as a game), you’re better off with base Xamarin.

However, if you’re building a straightforward app that doesn’t need much platform-specific functionality or custom UI, using Xamarin.Forms means you can write even less code. Data-entry apps, productivity tools, and prototypes are great candidates. Since the goal of this tutorial is building a simple demo app, you’ll use Xamarin.Forms here!

Xamarin Android Player For Mac

Create a New Xamarin.Forms Project

In Visual Studio, choose File - New Project, pick the Cross-Platform category, and choose the Cross-Platform App (Xamarin.Forms) template. Name the project HelloWorldApp.

Then, pick the Blank App template and the platforms you want to build the app for. Choose Xamarin.Forms as the UI Technology, and .NET Standard as the Code Sharing Strategy:

In Visual Studio for Mac, choose File - New Solution, pick the Multiplatform - App category, and choose the Blank Forms App template:

Creating the new project may take a few minutes. The Blank App template creates a solution with a few sub-projects:

  • HelloWorldApp: Contains the XAML and shared code for each platform-specific project.
  • HelloWorldApp.Android (or Droid): Android-specific code. For a simple project, you won’t have to change much here.
  • HelloWorldApp.iOS: iOS-specific code. You won’t have to change much here, either.

If you picked Windows (UWP) as a platform, your solution will contain an additional project targeting Windows devices.

In this tutorial, you’ll only need to modify the shared code project: HelloWorldApp.

Add a Page

UI views are called “pages” in Xamarin.Forms lingo, and your app already contains one called MainPage (or HelloWorldAppPage in Visual Studio for Mac). Double-click the XAML file in the Solution Explorer, and replace everything within the <ContentPage> tags with this markup:

This XAML markup creates a basic layout containing Label, Entry (text box), and Button elements. The element names (specified with x:Name) will be used to refer to these elements later in code. These XAML elements are generic and aren’t yet tied to a specific platform. Xamarin.Forms will automatically translate the elements in proper UIButton or EditText views when your app runs on iOS or Android.

The Clicked attribute on the Button element wires up the button click event to a handler called SayHelloButtonClicked, which doesn’t exist yet. You’ll write that next.

Add Code to the Page

Each XAML file is paired with a C# code file, sometimes called a “code-behind”. Open up the code for the MainPage.xaml (or HelloWorldAppPage.xaml) file by expanding it in the Solution Explorer and selecting the MainPage.xaml.cs file.

Below the public MainPage() method, add the new SayHelloButtonClicked method:

You may need to add the following declaration at the top of the file:

Because it’s referenced in the Clicked attribute, this method will run when the button is pressed or tapped on the XAML page. First, the value of the textbox is assigned to the name variable, and then the DisplayAlert method is called to display a modal alert on the device.

That’s it! Your new Xamarin app is ready to go. To test it, you can use a simulator, or you can use Xamarin Live Player to test it on a live device.

Test Your Xamarin App on Your Own Device

The quickest (and coolest) way to test a Xamarin project is with Xamarin Live Player, a small app you can download onto your own phone or device. After downloading the app, pair it with Visual Studio. Then, pick Live Player as the device target.

Duos-m Android Emulator

Start debugging by pressing the play icon, or choose Run - Start Debugging on the Mac. You’ll be asked to scan a QR code to pair Visual Studio with your device, and Live Player will connect to your computer. (If it hangs, make sure your computer and your device are on the same wi-fi network).

After Live Player connects, you’ll be able to immediately start using your app on your device! You can even make changes to the code in Visual Studio and Live Player will refresh the app on your device automatically. Super cool.

Test Your Xamarin App on Android

If you have the Visual Studio Android Emulator installed, testing the Android version of your Xamarin app is simple. In the Visual Studio toolbar, pick the HelloWorldApp.Android project and choose an Android device to emulate. Then, click the play button to start the emulator.

Xamarin Android Player Free Download

The Android emulator can be slow to load, so give it some time. If everything builds properly, you’ll see your app running on Android:

Test Your Xamarin App on iOS

Testing your Xamarin app on iOS is a little trickier, because it requires a Mac to provide the simulator. (Unless you’re already on a Mac and using Visual Studio for Mac, in which case, you’re good to go!)

If you’re on Windows and have a Mac handy, follow the official instructions to set up the Mac agent and connect it to Visual Studio. Then, pick the HelloWorld.iOS project, and switch the architecture to iPhone Simulator. Choose a device version and click play.

After the project builds, the simulator will launch on the Mac:

Next Steps

Xamarin

This tutorial only scratches the surface. There’s a ton more you can do with Xamarin!

Here’s some further reading:

  • Xamarin.Forms samples apps and code on GitHub

  • Developing Enterprise Apps using Xamarin.Forms and the accompanying free eBook

Do you want to learn more about Xamarin? What about other cross-platform app stacks like React Native, NativeScript, or Flutter? Let me know in the comments below!

Please enable JavaScript to view the comments powered by Disqus.