Play list of mp3 file with MediaPlayer in Android

后端 未结 2 942
余生分开走
余生分开走 2020-12-30 13:34

I have a problem to reproduce more than one mp3 file using MediaPlayer in Android. I\'m able to reproduce one single file but I did not find nothing useful to reproduce diff

相关标签:
2条回答
  • 2020-12-30 14:11

    May be the simplest solution is to implement setOnCompletionListener. Whenever your audio is completely played , this lifecycle is called and you can load and play next audio, so when the next audio is finished this lifecycle is called again, and on and on.

    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                     AssetFileDescriptor afd = null;
                    if (fileCounter == playlist.size() - 1) {
                        //for reloading playlist
                        fileCounter = 0;
                    }
                    if (fileCounter < playlist.size() - 1) {
                        try {
                            afd = getContext().getAssets().openFd("Audios/" + playlist.get(++fileCounter).getName() + ".mp3");
                            player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                            player.prepare();
                            player.start();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
    
                }
            });
    
    0 讨论(0)
  • 2020-12-30 14:26

    Create a list of the music that you want to include in playlist. Then keep track of the music that is playing and once finished start the next in the list.

    Like this

    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    
    import java.util.ArrayList;
    import java.util.Timer;
    import java.util.TimerTask;
    
    
    public class MainActivity extends AppCompatActivity {
    
        Timer timer;
        MediaPlayer mp;
        ArrayList<Integer> playlist;
        int i=0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            playlist = new ArrayList<>();
            playlist.add(R.raw.a1);
            playlist.add(R.raw.a2);
            mp = MediaPlayer.create(this,playlist.get(0));
            mp.start();
            timer = new Timer();
            if (playlist.size()>1) playNext();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        public void playNext() {
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    mp.reset();
                    mp = MediaPlayer.create(MainActivity.this,playlist.get(++i));
                    mp.start();
                    if (playlist.size() > i+1) {
                        playNext();
                    }
                }
            },mp.getDuration()+100);
        }
    
        @Override
        public void onDestroy() {
            if (mp.isPlaying())
                mp.stop();
            timer.cancel();
            super.onDestroy();
        }
    }
    
    0 讨论(0)
提交回复
热议问题