javaandroidandroid-assetsandroid-assetmanager

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


I am have a few wav files that I want to load with the help of Android's asset manager. Unfortunately, everytime I try to do so, I get the following error

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

I would like to add the following assets to the Android sound pool.

This is the image of my application set up.

Application IDE Set up

My code

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();
    }
  }

}

Fragment

    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;
   }
}

This method raises a FileNotFoundException.

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

This line precisely

AssetFileDescriptor afd = mAssetManager.openFd(soundName);

I have gone through the other posts related to this issue, but the most recent post is at least 3 years old. I don't know what I am doing wrong. Any help would be gratefully appreciated.


Solution

  • IIRC, list() returns the names of the items in the asset directory. It does not return the paths of those items.

    Try replacing:

    AssetFileDescriptor afd = mAssetManager.openFd(soundName);
    

    with:

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