Sophie
Sophie

Reputation: 2634

Audio Player - How to play next and prev songs using ListView

Writing a small Audio player in which i want to allow user to play prev and next songs whenever user do tap on respective button, i am using listview for listing all the songs

private int currentSongIndex = 0;

listview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
    long id) {                  

     strNURL = videoMovieArrayList.get(position).getUrl().toString();
     strNTITLE = videoMovieArrayList.get(position).getTitle().toString();

             if(mediaPlayer!=null && mediaPlayer.isPlaying())
                   try {
                           mediaPlayer.stop();
                        } catch (Exception e) {     
                    }
                          play(arg1);
                          textTrack.setText(strNTITLE);
                        }
                 });        
            }

    public void play(View view){    
       mediaPlayer = new MediaPlayer();
       mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(strNURL);
        } catch (IllegalArgumentException e) {
        .......................

      mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer arg0) {
            // TODO Auto-generated method stub
            pauseButton.setEnabled(false);
            playButton.setEnabled(true);                
           }
       });
   }

   public void prev(View view){ 
        Toast.makeText(MusicTrackActivity.this, "Previous song !", Toast.LENGTH_LONG).show();

       if(currentSongIndex > 0){
          // play(currentSongIndex - 1);
           currentSongIndex = currentSongIndex - 1;
       }else{
           // play last song
           // playSong(videoMovieArrayList.size() - 1);
           currentSongIndex = videoMovieArrayList.size() - 1;
       }
   }

   public void next(View view){ 
        Toast.makeText(MusicTrackActivity.this, "Next song !", Toast.LENGTH_LONG).show();

        if(currentSongIndex < (videoMovieArrayList.size() - 1)){        
             // play(currentSongIndex + 1);
            currentSongIndex = currentSongIndex + 1;
        }else{          
            // play(0);
            currentSongIndex = 0;
        }
   }

So now my query is How can i switch to Next song, whenever user do tap on next button, as you can see in above code, i am simply showing Toast message

Upvotes: 0

Views: 2983

Answers (2)

Waseem
Waseem

Reputation: 11

btnNext.setOnClickListener(new View.OnClickListener() {


             int  currentSongIndex= 0; 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                if(currentSongIndex < (songsList.size() - 1)){
                    playSong(currentSongIndex + 1);
                    currentSongIndex = currentSongIndex + 1;
                }else{
                    // play first song
                    plysong(songindex);
                }







            }
        });

Upvotes: 1

An SO User
An SO User

Reputation: 24998

You will need to keep track of the index of the song that is playing in a variable, say current.

Assuming your underlying data for the adapter stores URI of the songs in an ArrayList and the user presses 'next', check if current + 1 falls out of the ArrayList. If it does, don't play the song. However, if it doesn't, get the data from the ArrayList, reset your MediaPlayer and setDataSource() for your MediaPlayer.

Upvotes: 0

Related Questions