将音频文件添加到我的Android应用程序

时间:2013-12-12 16:48:12

标签: audio android

我正在尝试创建一个ar android应用程序。但我无法这样做。我尝试使用SoundPool,但音频文件永远不会播放。有人可以指导我如何使用soundpool吗?我是一个完全的初学者。

1 个答案:

答案 0 :(得分:2)

SoundPool用于播放小文件,通常短于30秒。你可以像这样构建一个SoundPool:

SoundPool player = new SoundPool(int maxStreams, int streamType, 0);

你加载一个声音文件:

int soundID = player.load(Context ctxt,int audioFile, int priority);

其中audioFile为R.raw.somesound(不包括文件扩展名)。请注意,您希望保留.load方法的返回值,否则您将无法播放声音。

你发出这样的声音:

int streamID = player.play(soundID, float leftVolume, float rightVolume ,int priority, int loop, float rate);

其中leftVolume和rightVolume从0到1浮动,不包括在内。您可能希望保持streamID暂停或稍后停止声音。请注意,您必须等到声音加载后才能播放(使用setOnLoadCompleteListener方法)

有关详细信息,请参阅the documentation