如何一个接一个播放音频文件 [英] 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?
我正在考虑使用 MediaPlayer
对象和 OnCompletionListener
s.但是,这意味着我必须创建很多 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.
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
}
});
这篇关于如何一个接一个播放音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!