我想一个接一个地播放音频

我想一个接一个地播放音频

本文介绍了当我单击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

推荐答案

以下是满足3个条件的解决方案:

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

当应用程序进入后台时释放MediaPlayer

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

这篇关于当我单击android中的按钮时,我想一个接一个地播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:00