gpt4 book ai didi

android - 当播放不止一种声音时如何停止媒体播放器?

转载 作者:行者123 更新时间:2023-12-02 08:15:45 24 4
gpt4 key购买 nike

我的应用程序是一个音乐板,它允许用户通过按下播放特定声音的按钮来创建自己的音乐,如果用户按下多个按钮,则会同时播放更多声音。

我想在调用 onPause() 时停止所有声音。但我意识到,如果同一个媒体播放器对象播放多个声音,它只会停止归因于它的最后一个声音,而其他声音则继续播放。

如果用户接到电话并且我的应用程序仍然在播放声音,这将是非常不愉快的:p

如有任何帮助,我们将不胜感激。

@Override
protected void onPause() {
super.onPause();

try {
while (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.release();
}
}catch (IllegalStateException e){

//Do nothing

}

}

编辑

我尝试实现 SoundPool API,但由于我的应用程序非常动态(用户可以上传他们的样本并使用麦克风进行录音)。我已经实现了所有内容并且运行良好,现在将其更改为另一个 API 而不是 MediaPlayer 会很困惑。 是否有任何方法可以停止所有归因于 MediaPlayer 对象且当前正在播放的声音?也许访问其他一些类并使用其他方法来停止所有声音?

最佳答案

我建议您查看 SoundPool API。看起来这很适合您的任务。它允许您预加载声音文件并独立播放它们。

The SoundPool class manages and plays audio resources for applications.

A SoundPool is a collection of samples that can be loaded into memory from a resource inside the APK or from a file in the file system. The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream. This allows applications to ship with compressed streams without having to suffer the CPU load and latency of decompressing during playback.

关于android - 当播放不止一种声音时如何停止媒体播放器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34051565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com