for loop using a raw music array android

后端 未结 4 816
再見小時候
再見小時候 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条回答
  •  萌比男神i
    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();
            }
        });
    }
    

    }

提交回复
热议问题