SantaZ
SantaZ

Reputation: 97

Using mp3 file in Xamarin.Forms

I added the *.mp3 file to the Resources/raw and don't know how to use it.

I want to start playing it in a media player:

public void Play()
{
    Stop();

    _player.Reset();
    string uri = "android.resource://Songer.Android/Resources/raw/test.mp3";
    _player.SetDataSource(uri);

    _player.Prepare();
    _player.Start();
}

But it does not find my file.

Upvotes: 0

Views: 2438

Answers (1)

Wendy Zang - MSFT
Wendy Zang - MSFT

Reputation: 10978

Play the mp3 file from resource folder, you could try the code below.

enter image description here

MediaPlayer mediaPlayer = MediaPlayer.Create(this, Resource.Raw.BuckBunnySound);
            mediaPlayer.Start();

The code you used works well on Assets folder.

enter image description here

            var player = new MediaPlayer();
            var fd = global::Android.App.Application.Context.Assets.OpenFd("BuckBunny.mp3");
            player.Prepared += (s, e) =>
            {
                player.Start();
            };
            player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
            player.Prepare();

Upvotes: 1

Related Questions