Setting an array of songs using MediaPlayer

前端 未结 3 1191
离开以前
离开以前 2021-02-10 16:43

I have 5 songs which i need to play one after the other, and it must loop from the first song after finishing the 5th song. How do I use the MediaPlayer to achieve this?

相关标签:
3条回答
  • 2021-02-10 17:00

    you need to use CountDownTimer for this. CountDown timer has give you facility to do this one by one ::

    MediaPlayer mp_xmPlayer2  = new MediaPlayer();
    mp_xmPlayer2 = MediaPlayer.create(this, R.raw.bg_music_wav);   
    
    
     for(int i=0;i<5;i++)
        {
        CountDownTimer cntr_aCounter = new CountDownTimer(3000, 1000) {
            public void onTick(long millisUntilFinished) {
    
                mp_xmPlayer2.start();
            }
    
            public void onFinish() {
                //change music name
              }
            };
            cntr_aCounter.start();
        }
    
    0 讨论(0)
  • 2021-02-10 17:03
    public class MediaPlayerExample extends Activity implements MediaPlayer.OnCompletionListener {
     int [] songs;
    MediaPlayer mediaPlayer;
    int current_index = 0; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    songs= new int[] {R.raw.song1,R.raw.song2,R.raw.song3,R.raw.song4};
    
    mediaPlayer = MediaPlayer.create(this, songs[0]);
    
    mediaPlayer.setOnCompletionListener(this);
    
    mediaPlayer.start();
    
    }
    @Override
        public void onCompletion(MediaPlayer mp) {
            play();
    
        }
    
     private void play()
        {
            current_index = (current_index +1)% 4;
            AssetFileDescriptor afd = this.getResources().openRawResourceFd(songs[current_index]);
    
            try
            {   
                mediaPlayer.reset();
                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
                mediaPlayer.prepare();
                mediaPlayer.start();
                afd.close();
            }
            catch (IllegalArgumentException e)
            {
                Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
            }
            catch (IllegalStateException e)
            {
                Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
            }
            catch (IOException e)
            {
                Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-10 17:08

    Simple:

    int[] songs = new int[]{R.raw.ab_hai_hamari_bari, R.raw.ab_khel_jamay_ga, R.raw.ab_khel_ke_dikha_psl_official_song, R.raw.chakka_choka_islamabad_united, R.raw.dil_dil_pakistan_junaid, R.raw.duniya_se_aagay, R.raw.hai_jazba_junoon_tho_himmat_na_haar, R.raw.hum_hain_pakistani_junaid, R.raw.jeet_ki_lagan, R.raw.jeetay_ga_pakistan_dedicate, R.raw.josh_e_junoon_pakistan_cricket, R.raw.karachi_kings_official, R.raw.lahore_qalandar_official, R.raw.multan_sultan_official, R.raw.peshawar_zalmi, R.raw.quetta_gladiator, R.raw.stand_up_for_the_champions};
    
    0 讨论(0)
提交回复
热议问题