Skip to content

Commit

Permalink
It all works by golly!
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ketchel committed May 12, 2023
1 parent 7eddc3c commit c39724a
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 394 deletions.
7 changes: 6 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<application
android:label="pttoc_test"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true"
>
<activity
android:name=".MainActivity"
android:exported="true"
Expand Down Expand Up @@ -41,4 +43,7 @@
</receiver>

</application>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
76 changes: 76 additions & 0 deletions lib/gui_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'package:flutter/material.dart';
import 'package:loggy/loggy.dart';

class GuiUtils with UiLoggy {
/// Wrapper function to provide the most common used form of snackbar, with theming support.
void showTextSnackbar(BuildContext? context, String message,
{bool isError = false,
SnackBarAction? action,
bool removeOthers = false}) {
if (context != null) {
ThemeData theme = Theme.of(context);
if (isError) {
ScaffoldMessenger.maybeOf(context)?.removeCurrentSnackBar();
ScaffoldMessenger.maybeOf(context)?.showSnackBar(SnackBar(
action: action,
backgroundColor: theme.colorScheme.error,
content: Row(
children: [
Icon(
Icons.error,
color: theme.colorScheme.onError,
),
const VerticalDivider(),
Text(
message,
style: TextStyle(color: theme.colorScheme.onError),
)
],
)));
} else {
ScaffoldMessenger.maybeOf(context)
?.showSnackBar(SnackBar(action: action, content: Text(message)));
}
} else {
loggy.warning(
"Asked to print text snackbar without context! Contents: $message");
}
}

/// Shows a snackbar with Widget contents;
///
/// Warning, colors may not properly apply to the [content] widget--you should style it yourself!
void showWidgetSnackbar(BuildContext? context, Widget content,
{bool isError = false,
SnackBarAction? action,
bool removeOthers = false}) {
if (context != null) {
ThemeData theme = Theme.of(context);
if (isError) {
ScaffoldMessenger.maybeOf(context)?.removeCurrentSnackBar();
ScaffoldMessenger.maybeOf(context)?.showSnackBar(SnackBar(
action: action,
backgroundColor: theme.colorScheme.error,
content: Row(
children: [
Icon(
Icons.error,
color: theme.colorScheme.onError,
),
const VerticalDivider(),
content,
],
)));
} else {
if (removeOthers) {
ScaffoldMessenger.maybeOf(context)?.removeCurrentSnackBar();
}
ScaffoldMessenger.maybeOf(context)
?.showSnackBar(SnackBar(action: action, content: content));
}
} else {
loggy.warning(
"Asked to print widget snackbar without context! Contents: ${content.toStringDeep()}");
}
}
}
16 changes: 16 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_loggy/flutter_loggy.dart';
// import 'package:logger/logger.dart' as LOGGER;
import 'package:loggy/loggy.dart';
import 'package:opus_dart/opus_dart.dart';
import 'package:opus_flutter/opus_flutter.dart' as opus_flutter;
Expand All @@ -9,6 +10,21 @@ import '/providers/mumble_provider.dart';
import '/views/mumble_ui/mumble_ui.dart';

void main() async {
// var logger = LOGGER.Logger(
// printer: LOGGER.PrettyPrinter(
// methodCount: 2, // number of method calls to be displayed
// errorMethodCount: 8, // number of method calls if stacktrace is provided
// lineLength: 120, // width of the output
// colors: true, // Colorful log messages
// printEmojis: true, // Print an emoji for each log message
// printTime: false // Should each log print contain a timestamp
// ),
// );
// Logger.addOutputListener((record) {
// print(
// '${record.time}: [${record.level.name}] ${record.loggerName}: ${record.message}');
// });

Loggy.initLoggy(
filters: [
// BlacklistFilter([NetworkLoggy])
Expand Down
93 changes: 7 additions & 86 deletions lib/providers/flutter_sound_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,19 @@ import 'dart:async';
import 'package:audio_session/audio_session.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:logger/logger.dart';
import 'package:permission_handler/permission_handler.dart';

const int tSampleRate = 44000;
typedef _Fn = void Function();

class FlutterSoundProvider extends ChangeNotifier {
FlutterSoundPlayer? _mPlayer = FlutterSoundPlayer();
FlutterSoundPlayer? _mPlayer = FlutterSoundPlayer(logLevel: Level.debug);
FlutterSoundRecorder? _mRecorder = FlutterSoundRecorder();
bool _mPlayerIsInited = false;
bool _mRecorderIsInited = false;
bool _mplaybackReady = false;
String? _mPath;
StreamSubscription? _mRecordingDataSubscription;

bool get isInitialized => _mPlayerIsInited && _mRecorderIsInited;

FlutterSoundProvider() {
// Be careful : openAudioSession return a Future.
// Do not access your FlutterSoundPlayer or FlutterSoundRecorder before the completion of the Future
// _mPlayer!.openPlayer().then((value) {
// _mPlayerIsInited = true;
// notifyListeners();
// });
// _openRecorder();
}

Future<void> initialize() async {
await _mPlayer!.openPlayer().then((value) {
_mPlayerIsInited = true;
Expand Down Expand Up @@ -69,87 +56,21 @@ class FlutterSoundProvider extends ChangeNotifier {

@override
void dispose() {
// stopPlayer();
_mPlayer!.stopPlayer();
_mPlayer!.closePlayer();
_mPlayer = null;

// stopRecorder();
_mRecorder!.stopRecorder();
_mRecorder!.closeRecorder();
_mRecorder = null;
super.dispose();
}
//
// Future<void> stopRecorder() async {
// await _mRecorder!.stopRecorder();
// if (_mRecordingDataSubscription != null) {
// await _mRecordingDataSubscription!.cancel();
// _mRecordingDataSubscription = null;
// }
// _mplaybackReady = true;
// }

get startRecorder => _mRecorder!.startRecorder;
get stopRecorder => _mRecorder!.stopRecorder;
// ---------------------- Here is the code to record to a Stream ------------

// Future<void> record() async {
// assert(_mRecorderIsInited && _mPlayer!.isStopped);
// var sink = await createFile();
// var recordingDataController = StreamController<Food>();
// _mRecordingDataSubscription =
// recordingDataController.stream.listen((buffer) {
// if (buffer is FoodData) {
// sink.add(buffer.data!);
// }
// });
// await _mRecorder!.startRecorder(
// toStream: recordingDataController.sink,
// codec: Codec.pcm16,
// numChannels: 1,
// sampleRate: tSampleRate,
// );
// setState(() {});
// }

// _Fn? getRecorderFn() {
// if (!_mRecorderIsInited || !_mPlayer!.isStopped) {
// return null;
// }
// return _mRecorder!.isStopped
// ? record
// : () {
// stopRecorder().then((value) => notifyListeners());
// };
// }

void play() async {
assert(_mPlayerIsInited &&
_mplaybackReady &&
_mRecorder!.isStopped &&
_mPlayer!.isStopped);
await _mPlayer!.startPlayer(
fromURI: _mPath,
sampleRate: tSampleRate,
codec: Codec.pcm16,
numChannels: 1,
whenFinished: () {
notifyListeners();
}); // The readability of Dart is very special :-(
notifyListeners();
}

Future<void> stopPlayer() async {
await _mPlayer!.stopPlayer();
}
get startPlayer => _mPlayer!.startPlayer;
get stopPlayer => _mPlayer!.stopPlayer;

// _Fn? getPlaybackFn() {
// if (!_mPlayerIsInited || !_mplaybackReady || !_mRecorder!.isStopped) {
// return null;
// }
// return _mPlayer!.isStopped
// ? play
// : () {
// stopPlayer().then((value) => notifyListeners());
// };
// }
FlutterSoundPlayer? get mPlayer => _mPlayer;
}
Loading

0 comments on commit c39724a

Please sign in to comment.