Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

android - How to play audio files one after the other

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 OnCompletionListeners. But, that would mean I have to create a lot of OnCompletionListeners because I need to know every time which audio file is next. Am I missing something or is there a better approach?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...