MediaPlayer freezing application in loop

ISSUE: I have problem with my code. I want to loop my MediaPlayer, but when I do it with while loop, MediaPlayer playing, but application is freezed. I know, mediaplayer have setLooping method, but I must do it with while loop because I need to change sound in code.

Do someone know what is solution for it? Thanks.

CODE:

    public void play() {
        while(true) {
            if (player == null) {
                player = MediaPlayer.create(context, R.raw.zvuk);
                player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        stopPlayer();
                    }
                });
            }
            player.start();
        }
    }

    public void stopPlayer() {
        if (player != null) {
                player.release();
                player = null;
        }
    }
}

Answer

you are using infinite loop, which will never end. Replace your code with this:

public void play() {
            if (player == null) {
                player = MediaPlayer.create(context, R.raw.zvuk);
                player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        // play again or write your logic for repeat 
                        play();
                    }
                });
            }
            player.start();
       
    }

    public void stopPlayer() {
        if (player != null) {
                player.release();
                player = null;
        }
    }