首页 > 解决方案 > 访问颤振资产时发生未找到文件异常

问题描述

我正在尝试编写一个插件来播放存储在颤振包的资产文件夹中的音频文件,并且这样做了

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

标签: javadartflutterfilenotfoundexception

解决方案


在您的 pubspec.yaml 文件中添加此...

flutter:
    assets:
-assets/flutter_assets/assets/river.m4a

推荐阅读