Skip to content Skip to sidebar Skip to footer

For Loop Using A Raw Music Array Android

I am currently trying to create a for loop in which it will play a raw file and when it's done, it will go on to the next sound file in the array. It is currently playing all files

Solution 1:

You can create separate MediaPlayer objects, start the first and then in onCompletion start the next. For a solution that scales, try this instead:

int[] myMusic = {R.raw.caralarm, R.raw.phonebusysignal, R.raw.phoneoffhook};
intmCompleted=0;

MediaPlayermp= MediaPlayer.create(this, myMusic[0]);

mp.setOnCompletionListener(newOnCompletionListener() {
    @OverridepublicvoidonCompletion(MediaPlayer mp) {
        mCompleted++;
        mp.reset();
        if (mCompleted < myMusic.length()) {
            try {
                AssetFileDescriptorafd= getResources().openRawResourceFd(myMusic[mCompleted]);
                if (afd != null) {
                    mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                    afd.close();
                    mp.prepare();
                }
            } catch (Exception ex) {
                // report a crash
            }
        } else {
            // done with media player
            mp.release();
            mp = null;
        }
    }
});

mp.start();

A simpler approach (slightly more wasteful of resources) is:

int[] myMusic = {R.raw.caralarm, R.raw.phonebusysignal, R.raw.phoneoffhook};
int mNext;
OnCompletionListenermListener=newOnCompletionListener() {
    @OverridepublicvoidonCompletion(MediaPlayer mp) {
        mp.release();
        startNextFile();
    }
};

@OverridepublicvoidonCreate() {
    // usual onCreate stuff here, then...// either here or whenever you want to start the sequence
    mNext = 0;
    startNextFile();
}

voidstartNextFile() {
    if (mNext < myMusic.length) {
        MediaPlayermp= MediaPlayer.create(this, myMusic[mNext++]);
        mp.setOnCompletionListener(mListener);
        mp.start();
    }
}

Solution 2:

This is working code for playing songs in continue loop.

publicclassMainActivityextendsActivity 
{
privateint[] tracks = {R.raw.explosion,R.raw.pianothingy_one,R.raw.car_horn_x};
intmCompleted=0;

publicvoidonCreate(Bundle savedInstanceState) 
{
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 MediaPlayermp= MediaPlayer.create(this, tracks[0]);
 mp.setOnCompletionListener(newOnCompletionListener() 
 {
     @OverridepublicvoidonCompletion(MediaPlayer mp)
     {
         mCompleted++;
         mp.reset();
         if (mCompleted < tracks.length) 
         {
             try
             {
                 AssetFileDescriptorafd= getResources().openRawResourceFd(tracks[mCompleted]);
                 if (afd != null) 
                 {
                     mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                     afd.close();
                     mp.prepare();
                     mp.start();
                 }
             }
             catch (Exception ex) 
             {
                ex.printStackTrace();
             }

         } 
         elseif (mCompleted>=tracks.length) 
         {
             mCompleted =0;
             try
             {
                 AssetFileDescriptorafd= getResources().openRawResourceFd(tracks[mCompleted]);
                 if (afd != null) 
                 {
                     mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                     afd.close();
                     mp.prepare();
                     mp.start();
                 }
             }
             catch (Exception ex) 
             {
                ex.printStackTrace();
             }
         }
         else
         {
             mCompleted=0;
              mp.release();
              mp = null;
         }

     }
 });

 mp.start();

Solution 3:

Will be easier to create an array of MediaPlayers, given you have only 3 of them. You should start only the first and set onCompletionListeners, where you'll start the next player. Hope this helps.

Solution 4:

int flag=0;
 int track; 

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);
    NextSongOnline()
    //here in my code i keept all sogns in araayList<> and by using mySongs i am parsing the arraylist that intented from another activity. //mySongs = (ArrayList) bundle.getParcelableArrayList("songs");
}


publicvoidNextSongOnLine(){
    if (flag == 0){
        track = position;
    }
    flag = 1;
    if (track == position){
        position = ((position + 1)%mySongs.size());
        Uriuri3= Uri.parse(mySongs.get(position).toString());
        myMediaPlayer = MediaPlayer.create(getApplicationContext(),uri3);

        sname = mySongs.get(position).getName().toString();
        songTitle.setText(sname);

        myMediaPlayer.start();
        seekBar.setMax(myMediaPlayer.getDuration()/1000);

        //set End time of music
        endTimeText.setText(createTimerLabel(myMediaPlayer.getDuration()/1000));

    }
    track++;
    myMediaPlayer.setOnCompletionListener(newMediaPlayer.OnCompletionListener() {
        // after one song completion this override method will be called with next position of song which is holding the counter "track". and that's how the loop will be continuing. @OverridepublicvoidonCompletion(MediaPlayer mp) {
            NextSongOnLine();
            Toast.makeText(getApplicationContext(),"Auto Playing Next Song",Toast.LENGTH_SHORT).show();
        }
    });
}

}

Post a Comment for "For Loop Using A Raw Music Array Android"