Android Asset Manager 无法从资产目录加载 wav 文件资产

Android Asset Manager unable to load wav file assets from the assets directory

我有几个要在 Android 的资产管理器的帮助下加载的 wav 文件。不幸的是,每次我尝试这样做时,我都会收到以下错误

java.io.FileNotFoundException: audio-file.wav not found

我想将以下资产添加到 Android 声音池中。

这是我的应用程序设置的图像。

Application IDE Set up

我的代码

public class MyActivity extends AppCompatActivity {

  protected abstract Fragment createFragment();

  @LayoutRes
  protected int getLayoutRes() {
    return R.layout.activity_fragment;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutRes());

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragment_container);
    if (fragment == null) {
      fragment = new MyActivityFragment();
      fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
    }
  }

}

片段

    public class MyActivityFragment extends Fragment {

       private static final String TAG = "MyFragment";
       private static final String SOUNDS_FOLDER = "sample_sounds";
       private static final int MAX_SOUNDS = 5;


    private final AssetManager mAssetManager;

      private final List<Integer> sounds = new ArrayList<>();
      private SoundPool mSoundPool; 

      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        this.mAssetManager = getActivity().getAssets();
        this.loadAssets();
        mSoundPool = this.buildSoundPool();
      }

      @Override
      public void onDestroy() {
        super.onDestroy();
        mSoundPool.release();
      }


  private SoundPool buildSoundPool() {
    SoundPool.Builder builder = new SoundPool.Builder();
    builder.setMaxStreams(MAX_SOUNDS);
    builder.setAudioAttributes(new AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_MEDIA)
        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
        .build());
    return builder.build();
  }

  private void loadAssets() {
    try {
      String[] soundNames = mAssetManager.list(SOUNDS_FOLDER);
      for (String soundName : soundNames) {
        sounds.add(load(soundName));
      }
    } catch (IOException ioe) {
      Log.e(TAG, "Could not list assets");
    }
  }

   private int load(String soundName) throws IOException {
      AssetFileDescriptor afd = mAssetManager.openFd(soundName);
      int soundId = mSoundPool.load(afd, 1);
      return soundId;
   }
}

此方法引发 FileNotFoundException

private int load(String soundName) throws IOException {
    AssetFileDescriptor afd = mAssetManager.openFd(soundName);
    int soundId = mSoundPool.load(afd, 1);
    return soundId;
  }

这条线正好

AssetFileDescriptor afd = mAssetManager.openFd(soundName);

我已经查看了与此问题相关的其他 post,但最近的 post 至少已有 3 年历史。我不知道我做错了什么。如有任何帮助,我们将不胜感激。

IIRC,list() returns 资产目录中项目的名称。它不 return 这些项目的 路径

尝试替换:

AssetFileDescriptor afd = mAssetManager.openFd(soundName);

与:

AssetFileDescriptor afd = mAssetManager.openFd(SOUND_FOLDER+"/"+soundName);