The correct way to play short sounds Android?

后端 未结 3 2026
离开以前
离开以前 2020-12-02 22:33

I am creating an application that will have multiple images on the screen, these images will be buttons and when tapped will play a short sound. I researched this and could

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 22:58

    MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
    
    try {
        if (mp.isPlaying()) {
            mp.stop();
            mp.release();
    
            mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
        }
    
        mp.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
    

提交回复
热议问题