O'Reilly logo

Xamarin Studio for Android Programming: A C# Cookbook by Mathieu Nayrolles

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Playing a song

There are an infinite number of occasions in which you would want your applications to play sound. In this section, you will learn how to play a song from our application in a user-interactive way (by means of clicking on a button) or programmative way.

Getting ready

Once again, we will reuse the project we created in the first recipe of this chapter.

How to do it…

Now, we will see how to play a song :

  1. Add the following using attribute to your MainActivity.cs file:
    using Android.Media;
  2. Create a class variable named _myPlayer in the MediaPlayer class:
    MediaPlayer _myPlayer;
  3. Create a subfolder named raw under the Resources folder.
  4. Place the sound you wish to play inside the newly created folder.

    We will use the Mario theme, free of rights for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required