简体   繁体   中英

Playing audio files one by one after setting them in a loop

I have approximately 6236 Audio files. What I am doing is that I am getting int values from the Database and setting them in loop. For Example after query I got 1 in 'a' and 7 in 'b'. Now I tried to used them in loop like for(int i=a;i<=b;i++) and passed it in my Path to play Audios one by one but 7 audios playing at a time. I am setting values dynamically from database in loop.What I want to do is to just play them one by one but I am stuck. Here is my Code:

    DatabaseAccess db=DatabaseAccess.getInstance(getApplicationContext());

    db.open();
    mycursor= db.getblindfirst(newid);
    scursor= db.getblindlast(newid);
    scursor.moveToFirst();
    mycursor.moveToFirst();
    a= mycursor.getInt(0);
     b=scursor.getInt(0);

                for (int i=a;i<=b;i++){
                try {


                    mPlayer.setDataSource("/mnt/sdcard/audio/aya(" + i + ").mp3");

                    mPlayer.prepare();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                mPlayer.start();

            }


    db.close();

}

That is because you are playing all the files at once. You should put the code that should run when the music is completed in the OnCompletionListener.

DatabaseAccess db=DatabaseAccess.getInstance(getApplicationContext());
db.open();
mycursor= db.getblindfirst(newid);
scursor= db.getblindlast(newid);
scursor.moveToFirst();
mycursor.moveToFirst();
a= mycursor.getInt(0);
b=scursor.getInt(0);
mPlayer = new MediaPlayer();
try {
    mPlayer.setDataSource("/mnt/sdcard/audio/aya(" + a + ").mp3");
    mPlayer.prepare();
} catch (Exception e) {
    e.printStackTrace();
}
mPlayer.start();
a++;
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        if (a!=b) {
            if(mPlayer != null) {
                if(mPlayer.isPlaying()) {
                    mPlayer.stop();
                }
                mPlayer.reset();
            }
            try {
                mPlayer.setDataSource("/mnt/sdcard/audio/aya(" + a + ").mp3");
                mPlayer.prepare();
            } catch (Exception e) {
                e.printStackTrace();
            }
            mPlayer.start();
            a++;
        }
    }
});
db.close();
}

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