SoundPool 未在 Android 应用程序中加载

发布于 2024-11-19 11:22:57 字数 808 浏览 1 评论 0原文

我正在制作一个应用程序,尝试将单个声音加载到 SoundPool 中,以便我可以在需要时管理播放速率,在需要时播放它,并在需要时释放它。但是,我的 SoundPool 未加载。我尝试过改变很多不同的事情,但似乎没有任何效果。我在 OnLoadComplete 函数中放置了一个 Log.d 来查看它是否加载,并且运行时没有出现日志...也许在查看我的代码后,其他人可以帮助我。谢谢!

private int currentVoice = 0;
private SoundPool voices = null;
....
public void onCreate(Bundle savedInstanceState) {
....
    voices = new SoundPool(1, 3, 0);
    voices.setOnLoadCompleteListener(this);
....
}
private void giveSentence() throws IOException {
....
voices.release();
currentVoice = voices.load(this, sentences[index].voice, 1);
voices.play(currentVoice, 1, 1, 1, 0, 1f);

public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
    soundPool.play(sampleId, 1, 1, 1, 0, 1f);
    boolean loaded = true;
    Log.d("Game","Is it loaded? " + loaded);
}

I'm making an app where I'm trying to load a single sound onto a SoundPool so I can manage the playback rate when I want, play it when I want, and release it when I want. However, my SoundPool is not loading. I've tried to change a lot of different things and nothing seems to work. I put a Log.d in the OnLoadComplete function to see if it loads, and the log doesn't appear when running... Maybe after looking at my code somebody else could help me. Thanks!

private int currentVoice = 0;
private SoundPool voices = null;
....
public void onCreate(Bundle savedInstanceState) {
....
    voices = new SoundPool(1, 3, 0);
    voices.setOnLoadCompleteListener(this);
....
}
private void giveSentence() throws IOException {
....
voices.release();
currentVoice = voices.load(this, sentences[index].voice, 1);
voices.play(currentVoice, 1, 1, 1, 0, 1f);

public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
    soundPool.play(sampleId, 1, 1, 1, 0, 1f);
    boolean loaded = true;
    Log.d("Game","Is it loaded? " + loaded);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷血 2024-11-26 11:22:57

您应该在 onCreate 中的某个位置初始化声音池 方法,我猜。

 private SoundPool voices;

 @Override
 public void onCreate(Bundle savedInstanceState) {
    voices = new SoundPool(1, 3, 0);
    voices.setOnLoadCompleteListener(this);
 }

You should initialize your sound pool somewhere in a onCreate method, I guess.

 private SoundPool voices;

 @Override
 public void onCreate(Bundle savedInstanceState) {
    voices = new SoundPool(1, 3, 0);
    voices.setOnLoadCompleteListener(this);
 }
鸢与 2024-11-26 11:22:57

修正了我自己,我正在做voices.release();在加载任何东西之前。由于某种原因,刚刚关闭了 SoundPool。在我检查它是否已经加载后,取出该行并执行 voices.release() 。现在可以了。

Fixed myself, I was doing voices.release(); before anything was loaded. For some reason that just turned the SoundPool off. Took out that line and did voices.release() after I checked if it was already loaded. Now it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文