summaryrefslogtreecommitdiffstats
path: root/lib/music_methods
diff options
context:
space:
mode:
Diffstat (limited to 'lib/music_methods')
-rw-r--r--lib/music_methods/controller.dart178
-rw-r--r--lib/music_methods/modeClass.dart15
-rw-r--r--lib/music_methods/mpd.dart20
-rw-r--r--lib/music_methods/musicProvider.dart64
4 files changed, 277 insertions, 0 deletions
diff --git a/lib/music_methods/controller.dart b/lib/music_methods/controller.dart
new file mode 100644
index 0000000..0ccd32b
--- /dev/null
+++ b/lib/music_methods/controller.dart
@@ -0,0 +1,178 @@
+// SPDX-License-Identifier: Apache-2.0
+import 'dart:async';
+import 'dart:io';
+
+import 'package:musicplayer/music_methods/mpd.dart';
+
+class MPDTalker {
+ String host;
+ int port;
+ // Socket socket;
+
+ MPDTalker({
+ this.host: "localhost",
+ this.port: 6600,
+
+ });
+
+ /**
+ * Send a request to MPD and get the output as a String.
+ */
+ Future<String> cmdStr(String cmd) {
+ Completer<String> com = new Completer<String>();
+ String data = "";
+
+ // Connect to MPD.
+ Socket.connect(host, port)
+ .then((Socket socket) {
+ // Write the command to MPD.
+ socket.write("$cmd\n");
+
+ // Listen for MPD.
+ socket.listen((List<int> chars) {
+ // Get the response.
+ String line = new String.fromCharCodes(chars);
+ data += line;
+
+ // Check if MPD is done sending data.
+ try {
+ if(_atEnd(line)) {
+ socket.close();
+ }
+ } catch(error, stacktrace) {
+ com.completeError(error, stacktrace);
+ }
+ },
+ onDone: () {
+ // Finish the future if there hasn't been an error.
+ if(!com.isCompleted) {
+ com.complete(data);
+ }
+ });
+ });
+
+ return com.future;
+ }
+
+ /**
+ * Check if MPD is done sending data.
+ */
+ bool _atEnd(String str) {
+
+ //str.contains(new RegExp("ACK \[[0-9]?([0-9])@[0-9]?([0-9])\]"));
+ if(str.contains("ACK [")) {
+ for(String line in str.split("\n")) {
+ if(line.startsWith("ACK [")) {
+ throw new MPDError(line);
+ break;
+ }
+ }
+ }
+
+ return str.endsWith("OK\n");
+ }
+
+ Future cmd(String str) {
+ Completer com = new Completer();
+ cmdStr(str).then((String data) {
+ com.complete();
+ });
+
+ return com.future;
+ }
+
+ /**
+ * Send a request to MPD and get the output as a List of Strings.
+ */
+ Future cmdList(String cmd) {
+ Completer com = new Completer();
+
+ // Get the data as a String, then turn it into a List.
+ cmdStr(cmd).then((String dataStr) {
+ List<String> data = [];
+
+ // For each line in the string:
+ for(String line in dataStr.split("\n")) {
+ // It will be separated into key/value pairs.
+ List<String> kv = line.split(":");
+
+ // We care about the value in this case, so add it to our list.
+ if(kv.length > 1)
+ data.add(kv[1].trim());
+ }
+
+ // Finished.
+ com.complete(data);
+ });
+
+ return com.future;
+ }
+
+ /**
+ * Send a request to MPD and get the output as a Map of Strings keyed to Strings.
+ */
+ Future<Map<String, String>> cmdMap(String cmd) async{
+ Map<String, String> data = new Map<String, String>();
+
+
+ // Get the data as a String, then turn it into a Map.
+ String dataStr = await cmdStr(cmd);
+
+ // For every line in the String:
+ for(String line in dataStr.split("\n")) {
+ // Split it into key value pairs like a map.
+ List<String> kv = line.split(":");
+ if(kv.length > 1) {
+ // Add our keys/values to the map.
+ data[kv[0].trim()] = kv[1].trim();
+ }
+ }
+
+
+
+ return data;
+ }
+
+ /**
+ * Send a request to MPD and get the output as a List of Maps of Strings keyed to Strings.
+ *
+ * String newKey: The key that defines a new item in the list.
+ */
+ Future<List<Map<String, String>>> cmdListMap(String cmd, {List<String>? newKeys}) async{
+ Completer com = new Completer();
+
+ // Set newKey's default to ["file"].
+ if(newKeys == null)
+ newKeys = ["file"];
+ List<Map<String, String>> data = [];
+
+
+ // Get the data as a String, then turn it into a List of Maps.
+
+
+ String dataStr = await cmdStr(cmd);
+
+
+ // For every line:
+ for(String line in dataStr.split("\n")) {
+ // Split it into keys/values.
+ List<String> kv = line.split(":");
+ if(kv.length > 1) {
+ // If the key is a new key, create a new Map in our list.
+ for(String key in newKeys) {
+ if(kv[0].trim() == key) {
+ data.add(Map<String, String>());
+ break;
+ }
+ }
+ // If we have Maps in our list, add the new item to the last Map.
+ if(data.isNotEmpty) {
+ data.last[kv[0].trim()] = kv[1].trim();
+ }
+ }
+ }
+
+ return data;
+
+ }
+} \ No newline at end of file
diff --git a/lib/music_methods/modeClass.dart b/lib/music_methods/modeClass.dart
new file mode 100644
index 0000000..1cb6122
--- /dev/null
+++ b/lib/music_methods/modeClass.dart
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: Apache-2.0
+class Mode{
+ Mode({required this.isRepeat, required this.isSingle, required this.prev});
+
+ final bool isSingle;
+ final bool isRepeat;
+ final bool prev;
+
+
+ Mode copyWith({bool? isSingle , bool? isRepeat, bool? prev}){
+ return Mode(isRepeat: isRepeat ?? this.isRepeat, isSingle: isSingle ?? this.isSingle, prev: prev ?? this.prev);
+ }
+
+
+} \ No newline at end of file
diff --git a/lib/music_methods/mpd.dart b/lib/music_methods/mpd.dart
new file mode 100644
index 0000000..124a5d4
--- /dev/null
+++ b/lib/music_methods/mpd.dart
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: Apache-2.0
+library MPD;
+
+import "dart:async";
+import "dart:io";
+
+
+
+
+/**
+ * An error for MPD.
+ */
+class MPDError extends Error {
+ String msg;
+ MPDError(this.msg) : super();
+
+ String toString() {
+ return "MPD Error: $msg";
+ }
+} \ No newline at end of file
diff --git a/lib/music_methods/musicProvider.dart b/lib/music_methods/musicProvider.dart
new file mode 100644
index 0000000..0e4d450
--- /dev/null
+++ b/lib/music_methods/musicProvider.dart
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: Apache-2.0
+import 'package:flutter/widgets.dart';
+import 'package:flutter_riverpod/flutter_riverpod.dart';
+import 'package:musicplayer/class.dart';
+import 'package:musicplayer/music_methods/modeClass.dart';
+
+final PositionProvider = StateNotifierProvider<Position,Duration>(((ref) => Position()));
+
+class Position extends StateNotifier<Duration>{
+ Position() : super(Duration(seconds: 0));
+ void update(value) async{
+ state = value;
+ }
+
+
+}
+
+final CurrentSongProvider = StateNotifierProvider<Current, CurrentSong>(((ref) => Current()));
+
+class Current extends StateNotifier<CurrentSong>{
+ Current() : super(currentval);
+ static final CurrentSong currentval = CurrentSong(title: 'title', artist: 'unknown', duration: '0min 0sec',isPlaying: false, time: '0');
+
+ void update({ String? title, String? artist, String? duration, bool? isPlaying, String? time}){
+ state = state.copyWith(title: title, artist: artist, duration: duration, isPlaying: isPlaying,time: time);
+ }
+
+}
+
+final VolumeProvider = StateNotifierProvider<Volume,int>(((ref) => Volume()));
+
+class Volume extends StateNotifier<int>{
+ Volume() : super(70);
+
+ void update(int val){
+ state = val;
+ }
+
+}
+
+final Modeprovider = StateNotifierProvider<mode,Mode>(((ref) => mode()));
+
+class mode extends StateNotifier<Mode>{
+ mode() : super(intial_value);
+ static final Mode intial_value = Mode(isRepeat: true, isSingle : false, prev: true);
+
+ void update({bool? isSingle, bool? isRepeat, bool? prev}){
+ state = state.copyWith(isRepeat : isRepeat, isSingle : isSingle,prev: prev);
+ }
+
+}
+
+final currIndexProvider = StateNotifierProvider<Index,int>(((ref) => Index()));
+
+class Index extends StateNotifier<int>{
+ Index() : super(0);
+
+ void update(int val){
+ state = val;
+ }
+
+}
+
+