0

I am simply trying to play a raw sound file and I am using prepare. The file is local, that is to say it is not retrieved via the Internet or anything.

I am unsure when I should use prepare() and when just directly play the file with .start()

I am successfully playing my background music like so:

private MediaPlayer mpInsidious;

mpInsidious = MediaPlayer.create(MainActivity.this, R.raw.insidious);

mpInsidious.start();

So far so good, but just below the .start() line, within the same activity onCreate() method I have the following:

MediaPlayer mp = new MediaPlayer();
mp.create(this, R.raw.creakingdoor);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }
});

However this file does not start, but again if I just setup my variable like so:

MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.creakingdoor);
mp.start();

It works as expected, but I was informed it is best to use prepare().

Can anyone tell me which way to go, and why the prepare way does not play the sound files?

andrewb
  • 2,995
  • 7
  • 54
  • 95

0 Answers0