0

我有一组声音文件名。我希望媒体播放器一个接一个地播放。但是,在循环播放时,它们都是同时播放的。解决方案是什么?

public static void PlaySentence(Context ctx, ArrayList<String> words)
{
    final ArrayList<MediaPlayer> players = new ArrayList<MediaPlayer>();

    for (String t : words)
    {           
        int sound_id = ctx.getResources().getIdentifier(t, "raw", ctx.getPackageName());

        if(sound_id != 0) {
            MediaPlayer newMP = MediaPlayer.create(ctx, sound_id);
            newMP.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {

                    players.remove(mp);
                    if (players.size() > 0)
                    {
                        Log.d("player", "size " + players.size());
                        players.get(0).start();
                    }

                    mp.release();
                }
            });

            players.add(newMP);
        }
    }

    players.get(0).start();
}

谢谢

4

1 回答 1

1

这段代码对我有用:

package com.example.ouio;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;

public class MainActivity extends Activity {

    private MediaPlayer mPlayer;
    private List<String> mQueuedFiles;

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

    public void onStart() {
        super.onStart();
        mPlayer = new MediaPlayer();
        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                System.out.println("OnCompletion called");
                playNextFile();
            }
        });
        mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                System.out.println("MediaPlayer error: " + what + " " + extra);
                mPlayer.release();
                mPlayer = new MediaPlayer();
                return false;
            }
        });
    }

    public void onStop() {
        super.onStop();
        mPlayer.release();
    }

    private AssetFileDescriptor getFileForWord(String word) {
        int sound_id = getResources().getIdentifier(word, "raw", getPackageName());

        return getResources().openRawResourceFd(sound_id);
    }

    private void playSentence(List<String> words) {
        mQueuedFiles = new ArrayList<String>();

        for (int i = 0; i < words.size(); i++) {
            mQueuedFiles.add(i, words.get(i));
        }

        playNextFile();     
    }

    private void playNextFile() {
        if (mQueuedFiles.size() == 0) {
            return;
        }

        AssetFileDescriptor afd = getFileForWord(mQueuedFiles.get(0));
        mQueuedFiles.remove(0);
        try {
            mPlayer.reset();
            mPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            mPlayer.prepare();
            mPlayer.start();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
于 2013-06-22T21:27:10.880 回答