i am trying to write a plugin to play audio files that are stored in assets folder of flutter package and have done like this
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 is correctly passed as
assets/river.m4a
i have checked and found out that the key which is looked up by registrar is
flutter_assets/assets/river.m4a
and the file is present in package at
assets/flutter_assets/assets/river.m4a
but still when i run the application it throws
D/TunePlugin: onMethodCall: exception occured java.io.FileNotFoundException: flutter_assets/assets/river.m4a