我正在尝试编写一个插件来播放存储在 flutter 包的 assets 文件夹中的音频文件,并且这样做了
if(call.method.equals("playMusic"))
{
Log.d(TAG, "onMethodCall: play music function called");
String fileLocation = call.argument("file");
Log.d(TAG, "onMethodCall: file requested is "+fileLocation);
AssetManager assetManager = registrar.context().getAssets();
String key = registrar.lookupKeyForAsset(fileLocation);
Log.d(TAG, "onMethodCall: key is "+key);
AssetFileDescriptor fd;
MediaPlayer mediaPlayer = new MediaPlayer();
try {
Log.d(TAG, "onMethodCall: found assets " + Arrays.toString(assetManager.list("assets/")));
fd= assetManager.openFd(key);
mediaPlayer.setDataSource(fd.getFileDescriptor(),fd.getStartOffset(),fd.getLength());
fd.close();
mediaPlayer.prepare();
mediaPlayer.start();
result.success("played successfully");
}
catch (Exception e){
Log.d(TAG, "onMethodCall: exception occured "+e.toString());
result.success("playing failed");
}
}
fileLocation 正确传递为
assets/river.m4a
我查了一下,注册商查到的key是
flutter_assets/assets/river.m4a
并且文件存在于包中
assets/flutter_assets/assets/river.m4a
但是当我运行应用程序时它仍然抛出
D/TunePlugin: onMethodCall: exception occured java.io.FileNotFoundException: flutter_assets/assets/river.m4a
最佳答案
在你的 pubspec.yaml 文件中添加这个......
flutter:
assets:
-assets/flutter_assets/assets/river.m4a
关于java - 访问flutter assets时出现File not Found异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52887774/