简体   繁体   中英

how do I move to the next array when the sound has finished playing?

im new here, can you help me? I want when the sound is complete, the imageView will display the next array of images

I have tried using this code but when the sound is finished it exits the application instead

    mediaPlayer = MediaPlayer.create(home.this, R.raw.decision);

    mediaPlayer.start();

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            next();
        }
    });

}

my code :

private int image [] = {R.drawable.definisiprisma1, R.drawable.definisiprisma2, R.drawable.definisiprisma3};
private int gambar = 0;
ImageView imageView;
MediaPlayer mediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    mediaPlayer = MediaPlayer.create(home.this, R.raw.decision);

    mediaPlayer.start();

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            next();
        }
    });

}

private void next() {
    gambar = gambar % image.length;
    gambar++;
    imageView.setImageResource(image[gambar]);
}

how do I move to the next array when the sound has finished playing?

屏幕启动时仅在第一次创建时调用,您可能想要再次启动声音。

You should initialize your imageView by

imageView = findViewById(R.id....);

in onCreate() method

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