배열에 음악 파일을 add를 통해서 넣고 리스트를 만든다.
아래 코드는 stackoverflow에 있는 해당 코드를 그대로 가져왔다. 코드를 참고해서 만들면 될 듯 하다.
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(); } }
https://stackoverflow.com/questions/30681002/play-list-of-mp3-file-with-mediaplayer-in-android