for loop using a raw music array android

后端 未结 4 800
再見小時候
再見小時候 2021-01-01 08:15

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 a

相关标签:
4条回答
  • 2021-01-01 08:33

    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.

    0 讨论(0)
  • 2021-01-01 08:37
     int flag=0;
     int track; 
    
    @Override
    protected void onCreate(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");
    }
    
    
    public void NextSongOnLine(){
        if (flag == 0){
            track = position;
        }
        flag = 1;
        if (track == position){
            position = ((position + 1)%mySongs.size());
            Uri uri3 = 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(new MediaPlayer.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. 
            @Override
            public void onCompletion(MediaPlayer mp) {
                NextSongOnLine();
                Toast.makeText(getApplicationContext(),"Auto Playing Next Song",Toast.LENGTH_SHORT).show();
            }
        });
    }
    

    }

    0 讨论(0)
  • 2021-01-01 08:44

    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();
    
    0 讨论(0)
  • 2021-01-01 08:46

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题