I am trying to make an audioplayer which will parse mp3s from a given folder which is preferably outside the app directory like "/storage/emulated/0/musicFiles" or "/sdcard/musicFiles".
Since I am not getting the whole functionality from Flutter I am making use of MethodChannel to call a method which will read the given pre-specified folder and return a List of the files which can be used by the Flutter ListView.builder to display a list.
My current source codes are as:
class _MyHomePageState extends State<MyHomePage> {
static const platform = const MethodChannel('player.fileHandler/getFileList');
List _responseFromNativeCode = [];
Future<void> responseFromNativeCode() async {
List response = [];
try {
final List result = await platform.invokeMethod('getFileList');
response = result;
} on PlatformException catch (e) {
print("${e.message}");
}
setState(() {
_responseFromNativeCode = response;
});
}
@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text("${_responseFromNativeCode.length}"),
/*RaisedButton(
child: Text('Call Native Method'),
onPressed: responseFromNativeCode,
),
//Text(_responseFromNativeCode[0]),
ListView.builder(
itemCount: _responseFromNativeCode.length,
itemBuilder: (context, index) {
return new Text(_responseFromNativeCode[index]);
},
),*/
],
),
),
);
}
}
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
getFileList();
}
);
}
private List<String> getFileList(){
File [] listOfFiles = new File("/sdcard/musicPlayer").listFiles();
List<String> list = new ArrayList<String>();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
list.add(listOfFiles[i].getAbsolutePath());
}
}
return list;
}
The code is supposed to return the paths as a list but the value shown usingText("${_responseFromNativeCode.length}")
is 0 which means for some reason either the value is not getting sent of I am having a problem in data types.
Any help would be very nice.
::
I think the MainActivity isn't being called. I put up several Log.d() to check the flow of the control and the content of the list but none of the logs were executed.
Assuming that your getFileList
method works as intended, then what you have to do is call result.success(getFileList())
inside your MethodCallHandler to receive the result in your dart code.
Read more about this here: https://flutter.dev/docs/development/platform-integration/platform-channels?tab=android-channel-java-tab#example
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.