2

I am making an instrument playing app ("Walkband" style) on Xamarin.Android for VS2017. I have an array of MediaPlayers called players.

This is the Play function, where resid is equivalent to Resource.Raw.filename:

public void Play(int resid)
    {
        for (int i = 0; i < players.Length; i++)
        {
            if (!players[i].IsPlaying)
            {
                players[i].Reset();
                players[i] = MediaPlayer.Create(this, resid);
                players[i].Start();
                break;
            }
        }
    }

Most of the time it plays great, but occasionally (specifically after playing many resources in quick succession), it fails to play.

The log looks like this when it succeeds:

06-04 14:52:26.112 I/MediaPlayer(17611): message received msg=2, ext1=0, ext2=0
06-04 14:52:26.112 I/MediaPlayer(17611): playback complete

And like this when it fails:

06-04 14:52:25.933 I/MediaPlayer(17611): message received msg=100, ext1=1, ext2=-19
06-04 14:52:25.933 E/MediaPlayer(17611): error (1, -19)
06-04 14:52:25.933 E/MediaPlayer(17611): Error (1,-19)

The resource itself isn't the issue, as all the resources mostly work, and all occasionally fail

sOfekS
  • 85
  • 6
  • Hi, does my answer work for you? – Robbit Jun 05 '18 at 08:17
  • Use `MediaPlayer`, you can exhaust the available resources on the device, even if you properly release all the players (which I don't see in your code). If the clips you are playing are short, then Joe's answer is right. – Dave Jun 05 '18 at 12:03

1 Answers1

1

I have an array of MediaPlayers called players.

You need to use soundpool, and please refer to this.

Robbit
  • 4,300
  • 1
  • 13
  • 29