在Android MediaPlayer中播放一个句子

时间:2013-06-22 08:03:27

标签: android android-mediaplayer

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

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

由于

1 个答案:

答案 0 :(得分: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();
        }
    }
}