【发布时间】:2014-02-03 20:31:22
【问题描述】:
在我们的 android 应用中,我们打开位于 assets/config/notification.wav 中的 wav 文件。要打开和播放声音,我们使用以下代码:
String soundClipPath = "config/notification.wav"
AssetFileDescriptor afd = mContext.getAssets().openFd(soundClipPath);
int soundID = mSoundPool.load(afd, 1);
这已经运行了大约一年,没有出现任何问题。突然,这停止了工作。我们尚未更新构建之间的任何依赖关系。我能找到的唯一区别是构建工作是从去年开始的。如果我们在 netbeans 中本地构建 apk,它可以工作,但是当我们使用 hudson 构建 apk 时它不起作用。我试过以下没有运气:
- 将 wav 文件放在 res/raw 文件夹中
- 在 maven android 插件中添加 -0
- 已验证文件结构并且文件存在并且可以正常工作。
使用上面的代码时,我们得到以下堆栈跟踪:
java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
at android.content.res.AssetManager.openAssetFd(Native Method)
at android.content.res.AssetManager.openFd(AssetManager.java:331)
...
尝试从 res/raw 文件夹加载时出现以下情况:
android.content.res.Resources$NotFoundException: File res/raw/notification.wav from drawable resource ID #0x7f040000
at android.content.res.Resources.openRawResourceFd(Resources.java:981)
at android.media.SoundPool.load(SoundPool.java:191)
...
谁能帮我们解决这个奇怪的问题....
最好的问候,
亨里克
【问题讨论】:
-
你的文件压缩了吗?
-
Android 中的资产是自动压缩的。 AssetManager 用于根据需要打开和解压缩文件。但是调用 openFd 就是这样做的。
-
您是否尝试过类似以下的方法:soundPool.load(afd.getFileDescriptor(), 0, afd.getLength(), 1) 或 afd.getFileDescriptor().valid() ?
-
@JiteshUpadhyay:文件未压缩,我尝试使用 -0 作为 maven-android-plugin 的参数,这意味着没有压缩。
-
@Peter 问题是我从来没有得到 afd 对象,因为它是抛出错误的 openFd() 方法
标签: android filenotfoundexception android-assets android-maven-plugin