1

I'm facing a problem in playing mp3 files in my android app. I have 50 mp3 files in the asset folder and what my app does is play them one by one when the button is clicked. The problem is my app stops playing audio after it has played around 30 mp3 files. Each mp3 file is shorter than 1 second. Thank you in advance.

Here is my code.

public class MainActivity extends AppCompatActivity {

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

        TextView textView1 = (TextView) findViewById(R.id.textView1);
        Button button1 = (Button) findViewById(R.id.button1);
        int cnt = 0;

        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                cnt++;
                playMP3(cnt + ".mp3");
                textView1.setText(cnt + "");

                if (cnt == 50) {
                    cnt = 0;
                }
            }
        });
    }

    private void playMP3(String filename){
        try {
            AssetFileDescriptor afd = getAssets().openFd(filename);
            MediaPlayer player = new MediaPlayer();
            player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            afd.close();
            player.prepare();
            player.start();
        } catch(Exception e) {
            e.printStackTrace();
        }

    }
}
pothny3
  • 138
  • 1
  • 2
  • 7
  • did you handle media player error case, might be one mp3 file was corrupted then mediaplayer stop playing and onError will called if you added listener – Vikas Tiwari May 08 '17 at 10:48
  • @VikasTiwari Thank you for your comment. I checked the android monior on Android Studio and it said "MediaPlayer error (-19,0)". I googled it and found the solution [here](http://stackoverflow.com/questions/9888510/mediaplayer-error-19-0-after-repeated-plays) – pothny3 May 08 '17 at 11:40

0 Answers0