简体   繁体   English

将音频文件设置为循环后,一个接一个地播放音频文件

[英]Playing audio files one by one after setting them in a loop

I have approximately 6236 Audio files. 我大约有6236个音频文件。 What I am doing is that I am getting int values from the Database and setting them in loop. 我正在做的是从数据库中获取int值并将其设置为循环。 For Example after query I got 1 in 'a' and 7 in 'b'. 例如,查询后我在“ a”中得到1,在“ b”中得到7。 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. 现在,我尝试像for(int i=a;i<=b;i++)这样在循环中使用它们,并将其传递到我的路径中以一一播放音频,但一次播放7个音频。 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. 您应该将在音乐播放完成后应该运行的代码放在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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM