问题描述
我在我的应用程序的资产目录中的多个音频文件。当用户点击一个按钮,我想打这些文件按照一定的顺序,一个接一个。不应该有音频文件之间的任何明显的滞后。什么是实现这一目标的最佳方法?
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?
我想用的MediaPlayer
对象和 OnCompletionListener
取值。但是,这将意味着我必须创造了很多 OnCompletionListener
的因为我需要知道每一个的音频文件是下一次。我缺少的东西,或是否有更好的方法?
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?
推荐答案
你是在正确的道路,并不需要很多OnCompletionListener's的。
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");
在你OnCompletionListener呢。
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
}
});
这篇关于如何在其他以后播放的音频文件之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!