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

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

java.io.FileNotFoundException:找不到audio-file.wav

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

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

应用程序IDE设置

我的代码

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

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

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

尝试更换:

AssetFileDescriptor afd = mAssetManager.openFd(soundName);

有:

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

转载请注明来自askonline.tech,本文标题:java - Android Asset Manager无法从assets目录加载wav文件资产


 Top