vendredi 30 août 2019

MediaPlayer - play sound ONLY ONCE by if statement

I'm trying to play a sound ONLY ONCE when the device is leaned in the X axis (horizontally).

Currently the sound loops when leaned in X axis.

I've tried setLooping(false), but I think the problem is that the if-statement which initiates the sound keeps updating after mediaPlayer has already started. So even if it's not looping, it's running mediaPlayer.start over and over.

   mediaPlayer = MediaPlayer.create(this, R.raw.sound);
   mediaPlayer.setLooping(false);

// phone is leaned towards the X value

            if (event.values[0] > event.values[1] && event.values[0] > event.values[2]) {
                mediaPlayer.start();
            }

How do I make the sound play only once and then wait until "if (event.value..." is triggered again before playing the sound again?

Note: I'm new to coding so I apologise for any dumb mistakes. Thanks in advance for any help.

Aucun commentaire:

Enregistrer un commentaire