2013-06-22 68 views
0

我有一個聲音文件名的數組。我希望媒體播放器依次播放每一個。但是,循環時,它們都是同時播放的。 這是什麼解決方案?在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(); 
} 

感謝

+0

顯示你的代碼 – stinepike

+0

您需要在您的媒體播放器設置'onCompletionListener'並播放當前的下軌oncompletion 。 http://developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener.html –

+0

請參閱我的新代碼。它在中間停止播放。有什麼問題 – Himberjack

回答

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