Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
I want to play audio one after another when i click button in android
ArrayList<String> videolist = new ArrayList<>();
videolist.add("http://muurl.com/abc/song1.mp3");
videolist.add("http://muurl.com/abc/song2.mp3");
videolist.add("http://muurl.com/abc/song3.mp3");

I have stored the audio links in the array list. when I click the button, I want to play that audio one after another, when I click the button second-time audio should start from the first audio link

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Have a look at the MediaPlayer class in conjunction with a OnCompletionListener:

You would do something like:

int playListPos = 0; // declare this outside the button click probably as a global variable (so we can access it and increment in the on click listener of the button

// the below code should go inside the button click
String url = videolist.get(playListPos); // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioAttributes(
    AudioAttributes.Builder()
        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
        .setUsage(AudioAttributes.USAGE_MEDIA)
        .build()
);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
    // this will be called every time a file is finished playing
    if (videolist.size() < playListPos) { // let's see if there is more files to play
        mediaPlayer.setDataSource(videolist.get(playlistPos));
        mediaPlayer.prepare();
        mediaPlayer.start();
        playListPos++;
    } else {
         // we played until the end. reset to 0 for when button is clicked again to restart from the beginning
         playListPos = 0;
   }
});
mediaPlayer.start();
over 4 years ago · Santiago Trujillo Report

0

Here is the solution that satisfies 3 conditions:

  • When user click Start button first time, the app will play video list one after one

  • When user click Start button second time, the app will play the first video in the video list

  • Release MediaPlayer when the app goes to background

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private MediaPlayer mediaPlayer;
    private List<String> videolist;
    private int currentPlayingPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity);

        videolist = new ArrayList<>();
        videolist.add("http://muurl.com/abc/song1.mp3");
        videolist.add("http://muurl.com/abc/song2.mp3");
        videolist.add("http://muurl.com/abc/song3.mp3");

        // User click this button to play video in video list
        Button buttonPlayVideoList = findViewById(R.id.buttonPlayVideoList);
        buttonPlayVideoList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playVideoList();
            }
        });
    }

    private void playVideoList() {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setLooping(false);
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    ++currentPlayingPosition;
                    if (currentPlayingPosition < videolist.size()) {
                        playVideoAtPosition(currentPlayingPosition);
                    }
                }
            });
        }
        currentPlayingPosition = 0;
        playVideoAtPosition(currentPlayingPosition);
    }

    private void playVideoAtPosition(int position) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(videolist.get(position));
            mediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStop() {
        // Release media player when app goes to background
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
        super.onStop();
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!