gpt4 book ai didi

java - Android 资源管理器无法从资源目录加载 wav 文件资源

转载 作者:行者123 更新时间:2023-12-02 05:12:38 28 4
gpt4 key购买 nike

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

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

}

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

此方法引发 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() 返回 Assets 目录中项目的名称。它不会返回这些项目的路径

尝试替换:

AssetFileDescriptor afd = mAssetManager.openFd(soundName);

与:

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

关于java - Android 资源管理器无法从资源目录加载 wav 文件资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56317181/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com