Beispiel #1
0
  private void randomizeMusic() {
    int random;
    // If its not the first time randomizing
    if (currentMusic != -1) {
      random = rand.nextInt(musicFileNames.length - 1);

      // To make sure the same music dosen't play twice
      if (currentMusic <= random) {
        random++;
      }
    } else {
      random = rand.nextInt(musicFileNames.length);
    }

    if (music != null) {
      music.stop();
      music.dispose();
    }

    currentMusic = random;
    music = Gdx.audio.newMusic(Gdx.files.internal("audio/" + musicFileNames[random]));
    music.play();
    music.setVolume(1);

    music.setOnCompletionListener(
        new OnCompletionListener() {
          @Override
          public void onCompletion(Music music) {
            randomizeMusic();
            music.dispose();
          }
        });
  }
  private void startLevel(String filename) {
    level = Level.loadLevel(filename);
    nextFootstep = 0;
    music = Gdx.audio.newMusic(Gdx.files.internal(level.getMusicFiles().poll()));
    music.play();
    final GameScreen thisGameScreen = this;
    music.setOnCompletionListener(
        new OnCompletionListener() {

          @Override
          public void onCompletion(Music music) {
            music.dispose();
            level.incrementCurrentSection();

            String nextMusicName = level.getMusicFiles().poll();
            if (!endingLevel) {
              if (nextMusicName != null) {
                Music nextMusic = Gdx.audio.newMusic(Gdx.files.internal(nextMusicName));
                nextMusic.play();
                nextMusic.setOnCompletionListener(this);
                thisGameScreen.music = nextMusic;
              } else {
                // End of level!
                // Play end of level sound
                // After it ends, load level 2
                Music endSound = Gdx.audio.newMusic(Gdx.files.internal("endOfLevel.ogg"));
                endSound.play();
                // final OnCompletionListener thisCompletionListener =
                // this;
                // this;
                endSound.setOnCompletionListener(
                    new OnCompletionListener() {

                      @Override
                      public void onCompletion(Music music) {
                        music.dispose();
                        startLevel("theme2.txt");
                      }
                    });
              }
            }
          }
        });
  }