Search code examples
androidnullpointerexceptionmedia-player

NullPointerException with MediaPlayer


Here is my code:

MediaPlayer a1,a2,a3...a24;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.a);
    a1 = MediaPlayer.create(this, R.raw.a1);
    a2 = MediaPlayer.create(this, R.raw.a2);
    a3 = MediaPlayer.create(this, R.raw.a3);
             ...
    a24 = MediaPlayer.create(this, R.raw.a24);
}

private void play(MediaPlayer p) {
    p.start();
}

The problem is that if I'm playing a1,a2,a3 everything is right, but if I'm playing a24 I get a NullPointerException. The size of the 24 audio files are about 25Kb each, so I don't think is a problem of memory consumption. I have tried putting a delay before starting play but nothing seems to help. The android documentation does not explain much about this; what should I do?


Solution

  • Did you actually check the value of a24 after calling create()? Is it non NULL? I think you are running into a limit on the number of MediaPlayer objects that can be created. There is a great post on the MediaPlayer and SoundPool here: http://www.stealthcopter.com/blog/2010/08/android-soundpool-vs-mediaplayer-focus-on-soundboards-and-memory-problems/