简体   繁体   中英

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

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();

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM