【问题标题】:FileNotFoundException when opening wav file from android assets从android资产打开wav文件时出现FileNotFoundException
【发布时间】: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


【解决方案1】:

要从原始资源文件夹播放它,我建议你需要这样的东西:

MediaPlayer mPlayer = MediaPlayer.create(PlayWorld.this, R.raw.notification);

然后您可以使用 MediaPlayer。 在您发布的第二个异常中,我看到了一些奇怪的东西:“来自 drawable 资源 ID 的文件 res/raw/notification.wav”

【讨论】:

    【解决方案2】:

    让我猜猜,您是否使用maven-jarsigner-plugin 进行应用程序签名?这个没有指定版本? 大约四个星期前,maven-jarsigner-plugin 已更新到 1.3 版,现在当我签署它时它对我的应用程序做了一些奇怪的事情 - 在应用程序启动后签名后,我遇到了像你这样的异常。 我解决了它只是在pom.xml 中的<artifactId>maven-jarsigner-plugin</artifactId> 之后添加<version>1.2</version>

    希望对你也有帮助。

    【讨论】:

    • 你是对的,maven-jarsigner-plugin 缺少版本标签。我会尝试添加它。谢谢!
    • 过去几天我们一直在努力解决这个问题。这个解决方案解决了我们的问题。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2011-07-02
    • 2015-04-08
    • 1970-01-01
    • 2013-12-06
    相关资源
    最近更新 更多