Test 2:
It has been a few months, forgot the Dart Syntax,
CPM from my old projects, flutter Cookbook, StackOverflow and Everywhere.
So, a messy, ha.
EventChannel changes, using Dictionary for message
like this, iOS side
// MARK:- Implement AudioManagerProtocol (Event Channel)
func relayTimeStamp(_ str : String)
{
let dict: [String: String] = ["TimeStamp": str ]
_eventSink?(dict)
}
func relayPowerLevel(_ dv : Double)
{
// not useful, limited by flutter stream timer (every secound only)
let dict: [String: Double] = ["PowerLevel": dv ]
_eventSink?(dict)
}
func relayThumbnail(_ uii: UIImage?)
{
guard uii != nil else
{
let dataRet = FlutterStandardTypedData.init(bytes: Data.init() )
let dict: [String: FlutterStandardTypedData] = ["Thumbnail": dataRet ]
_eventSink?(dict)
return
}
guard let imageData = uii!.jpegData(compressionQuality: 0.95) else { return }
let dataRet = FlutterStandardTypedData.init(bytes: imageData )
let dict: [String: FlutterStandardTypedData] = ["Thumbnail": dataRet ]
// print("did sink")
_eventSink?(dict)
}
-------------
main.dart, Dart side
-------------
void _onEvent(Object event)
{
var mapDv = event as Map*dynamic, dynamic*;
if (mapDv != null )
{
// Power Level: (not useful, at 1 fps)
final dv = mapDv["PowerLevel"] as double;
if (dv != null)
{
setState(() { _powerDv = dv; });
return;
}
// Top Row: Song's Thumbnail
final bytes = mapDv["Thumbnail"] as Uint8List;
if (bytes != null)
{
setState(() { _imgData = bytes; });
return;
}
// List, Auto Advance playback on EOF
final iTheIndex = mapDv["EOF"] as int;
if (iTheIndex != null)
{
int nextIdx = iTheIndex + 1;
if (nextIdx *= songArray.length ) nextIdx = 0;
_audioMgrIndex = nextIdx;
IosAudioPlugin.playSongAt(songArray[_audioMgrIndex].path, _audioMgrIndex);
setState(() {
nowPlaying = songArray[_audioMgrIndex].title;
});
scrollController.animateTo(itemHeight * _audioMgrIndex,
curve: Curves.easeInOut,
duration: Duration(milliseconds: 999));
return;
}
// Top Row: change icon Face of playPauseButton
final bool fbool = mapDv["IsPlaying"] as bool;
if (fbool != null)
{
setState(() { _isplaying = fbool; });
return;
}
// Top Row: timeStamp, duration, position, group
final str = mapDv["TimeStamp"] as String;
if (str != null)
{
setState(() { _platformVersion = str; });
}
// set Slider's max
// update slider position
} // end mapDv not null
}
Event Message , using Json String, so, serialize it on Swift Side
Deserialize it on Dart Size.
// MARK:- J s o n - S e r i a l i z a t i o n
// Dart
import 'dart:convert';
List*SongArray* songArrayFromJson(String str) =* List*SongArray*.from(json.decode(str).map((x) =* SongArray.fromJson(x)));
String songArrayToJson(List*SongArray* data) =* json.encode(List*dynamic*.from(data.map((x) =* x.toJson())));
class SongArray {
SongArray({
this.artist,
this.genre,
this.length,
this.text,
this.path,
this.title,
this.album,
});
String artist;
String genre;
String length;
String text;
String path;
String title;
String album;
factory SongArray.fromJson(Map*String, dynamic* json) =* SongArray(
artist: json["Artist"],
genre: json["Genre"],
length: json["Length"],
text: json["Text"],
path: json["Path"],
title: json["Title"],
album: json["Album"],
);
Map*String, dynamic* toJson() =* {
"Artist": artist,
"Genre": genre,
"Length": length,
"Text": text,
"Path": path,
"Title": title,
"Album": album,
};
}
getFilesFromFileProvider2Json2Array() async
{
String jsonString;
isLoaded = false;
isLoading = true;
try
{
jsonString = await IosAudioPlugin.getSongAsJsonString;
songArray = songArrayFromJson(jsonString);
isLoaded = true;
setState(() { });
}
catch (ex) { print(ex); }
isLoading = false;
}
Resources:
---------
A.S. 4.1.1
Xcode 12.1
FlutterDart
Testing Mp3 files
----------------
Folder Picker idea come from:
Tautvydas Šidlauskas
https://github.com/sidlatau/flutter_d...
which pick file.