Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Problem with Android.Media.MediaPlayer


I am building an app for Android using Xamarin.Forms.

One of the requirements of the app is to play a sound file (MP3) based on what is taking place in the app. For example, after it executes a particular block of code, if everything was successful it should play "SUCCESS.mp3", if partly successful, it should play "PARTSUCCESS.mp3", if everything fails, it should play "ERROR.mp3".

I came across this page
This page is Xamarin's official explanation of how this should be done. So, I've copied the files SUCCESS.mp3, PARTSUCCESS.mp3, and ERROR.mp3 into the Assets directory of the Android project.

Here is my code to play the sound (adapted from the example I received at the URL stated above):

protected Android.Media.MediaPlayer player;

    public void PlaySound(string filePath)
        if (player == null)
            player = new Android.Media.MediaPlayer();
        var fileDesc =  Xamarin.Forms.Forms.Context.Assets.OpenFd(filePath);            
        player.SetDataSource(fileDesc.FileDescriptor );

When I call the PlaySound method and pass a filename as argument (for example "ERROR.mp3"), I want the device to play only that file. However, this is not the case; the device plays every sound file in the Assets folder. It looks like it read all the filenames from the Assets folder and automatically created a playlist. Does anyone know how I can avoid that? I want the device to play only that sound file that I specify as argument to the PlaySound method.

Thank you very much.

Best regards


Best Answer


  • Terence.6876Terence.6876 USMember ✭✭

    @Namyslaw, thanks a lot. The tip worked.

Sign In or Register to comment.