- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题与this one有关.
我设法修改我的代码以使用 SoundPool 而不是 AudioManager。知道它或多或少会起作用。
public class Sound {
private static boolean sound = true;
private static final int EAT_SOUND = 1;
private static final int SHORT_EAT_SOUND = 2;
private static final int EAT_CHERRY_SOUND = 3;
private static final int EAT_GHOST_SOUND = 4;
private static final int EXTRA_LIVE_SOUND = 5;
private static final int INTERMISSION_SOUND = 6;
private static final int OPENING_SOUND = 7;
private static final int PACMAN_DIES_SOUND = 8;
private static final int SIREN_SOUND = 9;
private static Context context;
private static SoundPool soundPool;
private static HashMap<Integer, Integer> soundPoolMap;
public static void initializeOpenSound(Context con) {
context = con;
soundPool = new SoundPool(2, AudioManager.STREAM_RING, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(OPENING_SOUND, soundPool.load(context, R.raw.opening_song, 1));
}
public static void initializeSounds() {
soundPoolMap.put(EAT_SOUND, soundPool.load(context, R.raw.eating, 1));
soundPoolMap.put(SHORT_EAT_SOUND, soundPool.load(context, R.raw.eating_short, 1));
soundPoolMap.put(EAT_CHERRY_SOUND, soundPool.load(context, R.raw.eating_cherry, 1));
soundPoolMap.put(EAT_GHOST_SOUND, soundPool.load(context, R.raw.eating_ghoasts, 1));
soundPoolMap.put(EXTRA_LIVE_SOUND, soundPool.load(context, R.raw.extra_lives, 1));
soundPoolMap.put(INTERMISSION_SOUND, soundPool.load(context, R.raw.intermission, 1));
soundPoolMap.put(PACMAN_DIES_SOUND, soundPool.load(context, R.raw.pac_man_dies, 1));
soundPoolMap.put(SIREN_SOUND, soundPool.load(context, R.raw.siren, 1));
}
private static void playSound(int sound) {
int streamID = soundPool.play(soundPoolMap.get(sound), 0.5f, 0.5f, 1, 0, 1f);
if (sound == OPENING_SOUND) {
soundPool.setLoop(streamID, 2);
}
}
public static void playSirenSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(SIREN_SOUND)) {
System.out.println("Play Siren sound");
playSound(SIREN_SOUND);
} else {
throw new SoundInitializationError("Siren Sound not initialized!");
}
}
}
public static void playPacmanDiesSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(PACMAN_DIES_SOUND)) {
System.out.println("Play Pacman Dies sound");
playSound(PACMAN_DIES_SOUND);
} else {
throw new SoundInitializationError("Pacman Dies Sound not initialized!");
}
}
}
public static void playOpeningSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(OPENING_SOUND)) {
System.out.println("Play Opening sound");
playSound(OPENING_SOUND);
} else {
throw new SoundInitializationError("Opening Sound not initialized!");
}
}
}
public static void playIntermissionSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(INTERMISSION_SOUND)) {
System.out.println("Play Intermission sound");
playSound(INTERMISSION_SOUND);
} else {
throw new SoundInitializationError("Intermission Sound not initialized!");
}
}
}
public static void playExtraLiveSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(EXTRA_LIVE_SOUND)) {
System.out.println("Play Extra Live sound");
playSound(EXTRA_LIVE_SOUND);
} else {
throw new SoundInitializationError("Extra Live Sound not initialized!");
}
}
}
public static void playEatSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(EAT_SOUND)) {
System.out.println("Play Eat Sound");
playSound(EAT_SOUND);
} else {
throw new SoundInitializationError("Eat Sound not initialized!");
}
}
}
public static void playShortEatSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(SHORT_EAT_SOUND)) {
System.out.println("Play Short Eat sound");
playSound(SHORT_EAT_SOUND);
} else {
throw new SoundInitializationError("Short Eat Sound not initialized!");
}
}
}
public static void playEatCherrySound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(EAT_CHERRY_SOUND)) {
System.out.println("Play Eat Cherry sound");
playSound(EAT_CHERRY_SOUND);
} else {
throw new SoundInitializationError("Eat Cherry Sound not initialized!");
}
}
}
public static void playEatGhostSound() throws SoundInitializationError {
if (isSoundOn()) {
if (soundPoolMap.containsKey(EAT_GHOST_SOUND)) {
System.out.println("Play Eat Ghost sound");
playSound(EAT_GHOST_SOUND);
} else {
throw new SoundInitializationError("Eat Ghost Sound not initialized!");
}
}
}
public static boolean isSoundOn() {
return sound;
}
public static void setSoundOn(boolean b) {
sound = b;
}
}
但是,我有一个或多或少的大音频文件,我想在启动时播放。但是,如果我调用 initializeOpenSound() 然后调用 playOpening Sound(),则不会播放声音并且 logcat 输出:
07-15 09:11:02.121: WARN/SoundPool(14273): sample 1 not READY
在 SDK 2.2 中我会使用 SoundPool.OnLoadCompleteListener但如何使用 SDK 2.1 实现同样的效果?
编辑:我也很乐意提供一个不包含监听器或类似内容的解决方案,但要么尝试播放声音文件直到成功。
最佳答案
根据我的阅读,如果您尝试在初始化后立即播放声音,它不会准备好并且您会收到此错误。您可以制作一个加载页面,然后等待几秒钟,直到您确定声音已加载。
还要确保您的文件大小不超过 1048576 字节,这是 Soundpool 的限制。如果你想播放背景音乐或其他东西,我认为你必须使用媒体播放器。
关于java - 我如何知道 Soundpool 已准备好使用低于 2.2 的 SDK 目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3253108/
我正在细读 http://www.khronos.org/网站,只找到了 OpenCL 的头文件(不是我不关心的 OpenGL)。如何获取 OpenCL SDK? 最佳答案 AMD 的 ATI Str
Android 项目中最低(最低 sdk)和最高(目标 sdk)级别是否有任何影响。这些东西是否会影响项目的可靠性和效率。 最佳答案 没有影响,如果您以 SDK 级别 8 为目标,那么您的应用将以 9
“min sdk version/target sdk version”和“compile sdk version”有什么区别?我知道 min 和 target sdk 是什么意思,但是 compil
我正在尝试运行 Dji Mobile-Sdk-Android:https://github.com/dji-sdk/Mobile-SDK-Android使用 dji 网站上的说明:https://de
我目前正在向我的 iPhone 应用程序添加新的 Facebook iOS sdk 3.1.1。我看到 sdk 有重大变化。例如。我的旧 sdk 创建一个 Facebook 对象并从 FBReques
我最近刚刚下载了 Xcode 4.6 with mac 10.8.4 with iOS 6.0 SDK package,我立即注意到我无法使用我的 iPhone 3Gs with iOS 4.6 进行
我尝试下载 OpenCL SDK。但是没办法。我有一个 AMD GPU,所以我在谷歌上搜索了 AMD SDK,但是来自谷歌的所有链接和一些教程都被破坏了,不可能通过 AMD 开发者网站找到 sdk。
安装 Google Cloud SDK 后,当我运行时 gcloud 授权登录 我收到一条错误消息: Your browser has been opened to visit: https://a
我一直在 nvidia 网站上搜索 GPU 计算 SDK,因为我正在尝试构建具有 cuda 支持的点云库 (PCL)。但是,在 nvidia 网站上,我只能找到工具包的链接,而不是 SDK 的单个下载
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post一个问题。 2
当我打开 Android SDK 管理器时,会出现一个屏幕“选择要安装的包”。它列出了一堆带有绿色复选标记(已经安装)的软件包,还有一些带有 x's 的软件包,它们没有安装。如果我选择“全部接受”,它
在开发过程中,我发布了 SDK 21 Lollipop,但我无法在我的 KitKat 设备上使用它。应用程序非常简单,我只将 SDK 用于动画和 Material 设计,但是当我尝试对 Play 商店
enter image description here friend 们好 在使用 Linux、jenkins 和 docker 探索 dotnet 核心时,我遇到了构建问题,该问题在标题“/usr
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我创建了一个flutter项目,运行flutter packages get,输出如下:【二】flutter包搞定等待另一个 flutter 命令释放启动锁...第二次运行“flutter packa
这个问题在这里已经有了答案: What are the Android SDK build-tools, platform-tools and tools? And which version sh
我使用 Corona SDK 已经快一年了,并且开发了几个简单的游戏。我现在正在寻找的是在 Corona SDK 中创建 3D 幻觉的某种方法。如果有人有 Corona 3D 方面的经验,我将不胜感激
我有一个CLDC 1.1 / MIDP 2.0项目,可以使用Java ME SDK 3.2很好地进行编译。 最近,我已将Java SE升级到8,结果Java ME SDK开始在Windows启动时显示
有没有办法以编程方式控制连接到华擎主板的 RGB 照明?我知道华硕有一个 Aura SDK,但华擎是否也有一个用于他们的硬件? 如果是,我在哪里可以找到它? 最佳答案 没有用于华擎多彩的 sdk。但是
如何使用 crm 2011 sdk 和 XrmServiceContext 创建事务? 在下一个示例中,'new_brand' 是一些自定义实体。我想创建三个品牌。第三个拥有错误的 OwnerID g
我是一名优秀的程序员,十分优秀!