for loop using a raw music array android

2020-02-26 14:06发布

问题:

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 at once. Any suggestions as to how i could fix this? I think there might be a problem with the on completion listener, but not sure. Thanks.

my Code:

package com.example.mediatest;



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



    MediaPlayer mp = new MediaPlayer();



   for (int y =0; y<3; y++){
       mp = MediaPlayer.create(this, myMusic[y]);
       mp.start();
       mp.setOnCompletionListener(new OnCompletionListener(){

        @Override
        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub

        }

       });
   }

回答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};
int mCompleted = 0;

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

mp.setOnCompletionListener(new OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        mCompleted++;
        mp.reset();
        if (mCompleted < myMusic.length()) {
            try {
                AssetFileDescriptor afd = 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;
OnCompletionListener mListener = new OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.release();
        startNextFile();
    }
};

@Override
public void onCreate() {
    // usual onCreate stuff here, then...

    // either here or whenever you want to start the sequence
    mNext = 0;
    startNextFile();
}

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


回答2:

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.



回答3:

This is working code for playing songs in continue loop.

public class MainActivity extends Activity 
{
private int[] tracks = {R.raw.explosion,R.raw.pianothingy_one,R.raw.car_horn_x};
int mCompleted = 0;

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

 MediaPlayer mp = MediaPlayer.create(this, tracks[0]);
 mp.setOnCompletionListener(new OnCompletionListener() 
 {
     @Override
     public void onCompletion(MediaPlayer mp)
     {
         mCompleted++;
         mp.reset();
         if (mCompleted < tracks.length) 
         {
             try
             {
                 AssetFileDescriptor afd = 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 if (mCompleted>=tracks.length) 
         {
             mCompleted =0;
             try
             {
                 AssetFileDescriptor afd = 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();