How to Continue R.raw.audio sound after completing one loop by clicking on button in android

my program is working only once but i want to continue play sound every time, i have 98 audios, when i click on button and 1,2,3,.....97, 98 it's playing well but after 98,1,2,3 it's not playing sound in second loop. How to continue it's sound on next loop? your help will be appreciated. Thanks my Code:

public class MainActivity extends Activity {

private ImageView hImageViewPic;
private Button iButton, gButton;
MediaPlayer ourSong;

private int currentImage = 0;
private int currentAudio = 0;

int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
        R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7,
        R.drawable.p8, R.drawable.p9, R.drawable.p10, R.drawable.p11,
        R.drawable.p12, R.drawable.p13, R.drawable.p14, R.drawable.p15,
        R.drawable.p16, R.drawable.p17, R.drawable.p18, R.drawable.p19,
        R.drawable.p20, R.drawable.p21, R.drawable.p22, R.drawable.p23,
        R.drawable.p24, R.drawable.p25, R.drawable.p26, R.drawable.p27,
        R.drawable.p28, R.drawable.p29, R.drawable.p30, R.drawable.p31,
        R.drawable.p32, R.drawable.p33, R.drawable.p34, R.drawable.p35,
        R.drawable.p36, R.drawable.p37, R.drawable.p38, R.drawable.p39,
        R.drawable.p40, R.drawable.p41, R.drawable.p42, R.drawable.p43,
        R.drawable.p44, R.drawable.p45, R.drawable.p46, R.drawable.p47,
        R.drawable.p48, R.drawable.p49, R.drawable.p50, R.drawable.p51,
        R.drawable.p52, R.drawable.p53, R.drawable.p54, R.drawable.p55,
        R.drawable.p56, R.drawable.p57, R.drawable.p58, R.drawable.p59,
        R.drawable.p60, R.drawable.p61, R.drawable.p62, R.drawable.p63,
        R.drawable.p64, R.drawable.p65, R.drawable.p66, R.drawable.p67,
        R.drawable.p68, R.drawable.p69, R.drawable.p70, R.drawable.p71,
        R.drawable.p72, R.drawable.p73, R.drawable.p74, R.drawable.p75,
        R.drawable.p76, R.drawable.p77, R.drawable.p78, R.drawable.p79,
        R.drawable.p80, R.drawable.p81, R.drawable.p82, R.drawable.p83,
        R.drawable.p84, R.drawable.p85, R.drawable.p86, R.drawable.p87,
        R.drawable.p88, R.drawable.p89, R.drawable.p90, R.drawable.p91,
        R.drawable.p92, R.drawable.p93, R.drawable.p94, R.drawable.p95,
        R.drawable.p96, R.drawable.p97, R.drawable.p98, R.drawable.p99 };

int[] audios = {R.raw.a0 ,R.raw.a1, R.raw.a2, R.raw.a3, R.raw.a4, R.raw.a5,
        R.raw.a6, R.raw.a7, R.raw.a8, R.raw.a9, R.raw.a10, R.raw.a11,
        R.raw.a12, R.raw.a13, R.raw.a14, R.raw.a15, R.raw.a16, R.raw.a17,
        R.raw.a18, R.raw.a19, R.raw.a20, R.raw.a21, R.raw.a22, R.raw.a23,
        R.raw.a24, R.raw.a25, R.raw.a26, R.raw.a27, R.raw.a28, R.raw.a29,
        R.raw.a30, R.raw.a31, R.raw.a32, R.raw.a33, R.raw.a34, R.raw.a35,
        R.raw.a36, R.raw.a37, R.raw.a38, R.raw.a39, R.raw.a40, R.raw.a41,
        R.raw.a42, R.raw.a43, R.raw.a44, R.raw.a45, R.raw.a46, R.raw.a47,
        R.raw.a48, R.raw.a49, R.raw.a50, R.raw.a51, R.raw.a52, R.raw.a53,
        R.raw.a54, R.raw.a55, R.raw.a56, R.raw.a57, R.raw.a58, R.raw.a59,
        R.raw.a60, R.raw.a61, R.raw.a62, R.raw.a63, R.raw.a64, R.raw.a65,
        R.raw.a66, R.raw.a67, R.raw.a68, R.raw.a69, R.raw.a70, R.raw.a71,
        R.raw.a72, R.raw.a73, R.raw.a74, R.raw.a75, R.raw.a76, R.raw.a77,
        R.raw.a78, R.raw.a79, R.raw.a80, R.raw.a81, R.raw.a82, R.raw.a83,
        R.raw.a84, R.raw.a85, R.raw.a86, R.raw.a87, R.raw.a88, R.raw.a89,
        R.raw.a90, R.raw.a91, R.raw.a92, R.raw.a93, R.raw.a94, R.raw.a95,
        R.raw.a96, R.raw.a97, R.raw.a98 };

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    hImageViewPic = (ImageView) findViewById(R.id.idImageViewPic);
    iButton = (Button) findViewById(R.id.bIleri);
    gButton = (Button) findViewById(R.id.bGeri);
    // Just set one Click listener for the image

    iButton.setOnClickListener(iButtonChangeImageListener);
    gButton.setOnClickListener(gButtonChangeImageListener);

}


View.OnClickListener iButtonChangeImageListener = new OnClickListener() {

    public void onClick(View v) {
        try {
            // Increase Counter to move to next Image and Audio
            currentImage++;
            currentAudio++;


            currentImage = (currentImage + images.length) % images.length;

            hImageViewPic.setImageResource(images[currentImage]);

            ourSong = MediaPlayer.create(MainActivity.this,audios[currentAudio]);


            ourSong.start();

        } catch (Exception e) {

        }

    }

};


View.OnClickListener gButtonChangeImageListener = new OnClickListener() {

    public void onClick(View v) {
        try {
            // Decrease Counter to move to previous Image and Audio
            currentImage--;
            currentAudio--;

            currentImage = (currentImage + images.length) % images.length;

            hImageViewPic.setImageResource(images[currentImage]);

            ourSong = MediaPlayer.create(MainActivity.this, audios[currentAudio]);

            ourSong.start();

        } catch (Exception e) {

        }

    }
};  

}

Answers


I think you are missing rollover for currentAudio.

currentAudio = ++currentAudio % audios.length;
 AND
currentAudio = (--currentAudio + audios.length) % audios.length;

Need Your Help

Can't make changes to SQL backend from an MS Access Data Project

sql-server ms-access adp access-data-project

I have a MS Access 2010 Data Project (adp) connected to MS SQL Server 2008. Based on Microsoft's website, I should be able to modify the SQL backend from within the MS Access 2010 Data Project.

How to add Mathjax support to PHPMyAdmin

javascript mysql dom phpmyadmin mathjax

I use MySQL DBMS for storing my data. I've lot of mathematical data to enter into the database. I guess entering Mathematical data in the form of latex is a best option.(Please feel free to suggest...