添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
温柔的灭火器  ·  sparkconf参数设置-掘金·  1 年前    · 
冲动的红金鱼  ·  WPF ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have multiple audio files in the assets directory of my application. When the user clicks a button I want to play those files in a certain order, one after the other. There shouldn't be any noticeable lag between the audio files. What is the best approach to achieve this?

I am thinking of using MediaPlayer objects and OnCompletionListener s. But, that would mean I have to create a lot of OnCompletionListener s because I need to know every time which audio file is next. Am I missing something or is there a better approach?

you are on the right way, don't need a lot of OnCompletionListener´s.

//define a variable to be used as index.
int audioindex = 0;
//Extract the files into an array
String[] files = null;
files = assetManager.list("audiofiles");

then in your OnCompletionListener.

 mp.setOnCompletionListener(new OnCompletionListener(){
    // @Override
    public void onCompletion(MediaPlayer arg0) {
    // File has ended, play the next one.
   FunctionPlayFile(files[audioindex]);
   audioindex+=1; //increment the index to get the next audiofile

Check this, these classes can play mp3 urls one after another, i created them roughly at some point,and can be tweaked for playing from asset........

https://github.com/vivdub/AndroidMediaplayer

create raw folder in res directory and put your sound file there

Now... Use PlayMedia Like this

int[] soundIDs = {R.raw.yes, R.raw.eat};
PlayMedia playAudio = new PlayMedia(context,soundIDs);
playAudio.execute();

and define PlayMedia Class Like this

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.AsyncTask;
import android.util.Log;
public class PlayMedia extends AsyncTask<Void, Void, Void> {
    private static final String LOG_TAG = PlayMedia.class.getSimpleName();
    Context context;
    private MediaPlayer mediaPlayer;
    int[] soundIDs;
    int idx =1;
    public PlayMedia(MediaPlayer mediaPlayer) {
        this.mediaPlayer = mediaPlayer;
    public PlayMedia(final Context context, final int[] soundIDs) {
        this.context = context;
        this.soundIDs=soundIDs;
        mediaPlayer = MediaPlayer.create(context,soundIDs[0]);
        setNextMediaForMediaPlayer(mediaPlayer);
    public void setNextMediaForMediaPlayer(MediaPlayer player){
        player.setOnCompletionListener(new OnCompletionListener() {         
            public void onCompletion(MediaPlayer mp) {
                if(soundIDs.length>idx){
                    mp.release();
                    mp = MediaPlayer.create(context,soundIDs[idx]);
                    setNextMediaForMediaPlayer(mp);
                    mp.start();
                    idx+=1;
    @Override
    protected Void doInBackground(Void... params) {
        try {
            mediaPlayer.start();
        } catch (IllegalArgumentException e) {
            Log.e(LOG_TAG, "", e);
        } catch (SecurityException e) {
            Log.e(LOG_TAG, "", e);
        } catch (IllegalStateException e) {
            Log.e(LOG_TAG, "", e);
        return null;
                I tried it but the audio suddenly stops after 4-5 secs...do you know why is that happening?
– Parag Kadam
                Oct 12, 2015 at 9:52
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.