简体   繁体   中英

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?

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/

To answer your comment:

MediaPlayer mp = new MediaPlayer();

AssetFileDescriptor descriptor;

descriptor = getAssets().openFd( "track name" ); 
mp.setDataSource(     descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength() ); 
descriptor.close(); 
mp.setLooping(false); 
mp.prepare();
mp.start();

You would need to reset the next time and re-setup to play again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM