java - 访问颤振资产时发生未找到文件异常
问题描述
我正在尝试编写一个插件来播放存储在颤振包的资产文件夹中的音频文件,并且这样做了
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 正确传递为
资产/河流.m4a
我检查并发现注册商查找的密钥是
flutter_assets/assets/river.m4a
并且该文件存在于包中
资产/flutter_assets/资产/river.m4a
但是当我运行它抛出的应用程序时
D/TunePlugin:onMethodCall:发生异常 java.io.FileNotFoundException:flutter_assets/assets/river.m4a
解决方案
在您的 pubspec.yaml 文件中添加此...
flutter:
assets:
-assets/flutter_assets/assets/river.m4a
推荐阅读
- python-3.x - 如何在自动使用模式下获取 pytest 夹具返回值?
- python - 如何找到乘以浮点数列表时使所有值都为整数的最小整数?
- c# - 为什么允许我们在条件运算符的 else 子句中使用由“is”表达式定义的参数?
- python - 为什么我在 vscode 中的文件在尝试执行后一直被删除?
- laravel - 在 Laravel 中搜索和获取特定用户
- python - 如何比较体育游戏中python中相似字符串的列表?
- node.js - 如何从 log4js 中获取当前的实时日志数据?
- android-constraintlayout - 在 Constraintlayout 中水平对齐两个 TextView
- markdown - 使用 FrontMatter 来拉取 110 的相关帖子集合?
- sql - Row_Number 基于两列