-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
miki
committed
Oct 14, 2024
1 parent
7960304
commit 421da1e
Showing
36 changed files
with
1,834 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
bruig/flutterui/bruig/android/app/src/main/res/xml/network_security_config.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<network-security-config> | ||
<domain-config cleartextTrafficPermitted="true"> | ||
<domain includeSubdomains="false">127.0.0.1</domain> | ||
</domain-config> | ||
</network-security-config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
configurations.maybeCreate("default") | ||
artifacts.add("default", file('opus.aar')) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
bruig/flutterui/bruig/lib/components/audio_element.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:bruig/components/buttons.dart'; | ||
import 'package:bruig/components/snackbars.dart'; | ||
import 'package:bruig/components/text.dart'; | ||
import 'package:bruig/models/audio.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:just_audio/just_audio.dart'; | ||
|
||
class AudioPlayerTracker extends StatefulWidget { | ||
final Uint8List audioBytes; | ||
final AudioModel audio; | ||
const AudioPlayerTracker( | ||
{required this.audioBytes, required this.audio, super.key}); | ||
|
||
@override | ||
State<AudioPlayerTracker> createState() => _AudioPlayerTrackerState(); | ||
} | ||
|
||
class _AudioPlayerTrackerState extends State<AudioPlayerTracker> { | ||
AudioModel get audio => widget.audio; | ||
bool get playing => audio.playing && audio.playingSource == widget.audioBytes; | ||
bool listeningPosition = false; | ||
|
||
double progress = 0; | ||
|
||
void onPositionChanged() { | ||
var length = audio.audioPosition.length.inMilliseconds; | ||
var value = audio.audioPosition.position.inMilliseconds; | ||
|
||
// print("YYYYY onPositionChanged tracker $value $length"); | ||
|
||
if (length == 0) { | ||
return; | ||
} | ||
setState(() { | ||
progress = value / length; | ||
}); | ||
} | ||
|
||
void update() { | ||
if (!playing) { | ||
if (listeningPosition) { | ||
audio.audioPosition.removeListener(onPositionChanged); | ||
listeningPosition = false; | ||
} | ||
return; | ||
} | ||
|
||
if (!listeningPosition) { | ||
audio.audioPosition.addListener(onPositionChanged); | ||
listeningPosition = true; | ||
} | ||
|
||
if (audio.playerEvents.lastEvent.processingState == | ||
ProcessingState.completed) { | ||
if (listeningPosition) { | ||
print("BBBBBBB removing listener"); | ||
audio.audioPosition.removeListener(onPositionChanged); | ||
listeningPosition = false; | ||
} | ||
setState(() { | ||
progress = 1; | ||
}); | ||
} | ||
} | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
audio.playerEvents.addListener(update); | ||
update(); | ||
} | ||
|
||
@override | ||
void didUpdateWidget(covariant AudioPlayerTracker oldWidget) { | ||
super.didUpdateWidget(oldWidget); | ||
update(); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
audio.playerEvents.removeListener(update); | ||
if (listeningPosition) { | ||
audio.audioPosition.removeListener(onPositionChanged); | ||
} | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return LinearProgressIndicator(value: progress); | ||
} | ||
} | ||
|
||
class AudioElement extends StatefulWidget { | ||
final Uint8List audioBytes; | ||
final String mimeType; | ||
final AudioModel audio; | ||
const AudioElement( | ||
{super.key, | ||
required this.audioBytes, | ||
required this.mimeType, | ||
required this.audio}); | ||
|
||
@override | ||
State<AudioElement> createState() => _AudioElementState(); | ||
} | ||
|
||
class _AudioElementState extends State<AudioElement> { | ||
AudioModel get audio => widget.audio; | ||
bool playing = false; | ||
double playSpeed = 1.0; | ||
|
||
static const List<double> speeds = [0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2]; | ||
|
||
void playStop() async { | ||
if (playing) { | ||
try { | ||
audio.stop(); | ||
} catch (exception) { | ||
showErrorSnackbar(this, "Unable to stop audio: $exception"); | ||
} | ||
} else { | ||
try { | ||
await audio.player.setSpeed(playSpeed); | ||
await audio.playMemAudio(widget.mimeType, widget.audioBytes); | ||
} catch (exception) { | ||
showErrorSnackbar(this, "Unable to play audio: $exception"); | ||
} | ||
} | ||
} | ||
|
||
void setPlaySpeed(double? v) { | ||
if (v == null) { | ||
return; | ||
} | ||
setState(() => playSpeed = v); | ||
if (playing) { | ||
audio.player.setSpeed(v); | ||
} | ||
} | ||
|
||
void updated() { | ||
var newPlaying = audio.playingSource == widget.audioBytes && audio.playing; | ||
if (playing != newPlaying) { | ||
setState(() { | ||
playing = newPlaying; | ||
}); | ||
} | ||
} | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
audio.addListener(updated); | ||
updated(); | ||
} | ||
|
||
@override | ||
void didUpdateWidget(covariant AudioElement oldWidget) { | ||
super.didUpdateWidget(oldWidget); | ||
if (oldWidget.audio != audio) { | ||
oldWidget.audio.removeListener(updated); | ||
audio.addListener(updated); | ||
updated(); | ||
} | ||
} | ||
|
||
@override | ||
void dispose() { | ||
audio.removeListener(updated); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return (Row(children: [ | ||
CircularProgressButton( | ||
active: playing, | ||
inactiveIcon: Icons.play_arrow, | ||
activeIcon: Icons.stop, | ||
onTapDown: playStop), | ||
const SizedBox(width: 20), | ||
Expanded( | ||
child: | ||
AudioPlayerTracker(audio: audio, audioBytes: widget.audioBytes)), | ||
const SizedBox(width: 20), | ||
DropdownButton( | ||
value: playSpeed, | ||
items: speeds | ||
.map((v) => | ||
DropdownMenuItem<double>(value: v, child: Txt.S("${v}x"))) | ||
.toList(), | ||
onChanged: setPlaySpeed, | ||
) | ||
])); | ||
} | ||
} |
Oops, something went wrong.