Skip to content

Commit

Permalink
update.
Browse files Browse the repository at this point in the history
  • Loading branch information
duan authored and duan committed Nov 29, 2024
1 parent 3e814ec commit 6e1bad3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
19 changes: 7 additions & 12 deletions example/lib/widgets/sound_waveform.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:livekit_client/livekit_client.dart';
import 'package:livekit_example/theme.dart';

class SoundWaveformWidget extends StatefulWidget {
final int count;
Expand All @@ -27,25 +24,23 @@ class SoundWaveformWidget extends StatefulWidget {
class _SoundWaveformWidgetState extends State<SoundWaveformWidget>
with TickerProviderStateMixin {
late AnimationController controller;
List<double> samples = [0,0,0,0,0,0,0];
EventsListener<TrackEvent>? _listener;
List<double> samples = [0, 0, 0, 0, 0, 0, 0];
EventsListener<TrackEvent>? _listener;

void _startVisualizer(AudioTrack track) async {
await widget.audioTrack.startVisualizer();
_listener?.dispose();
await _listener?.dispose();
_listener = track.createListener();
_listener?.on<AudioVisualizerEvent>((e) {
if(mounted) {
if (mounted) {
setState(() {
samples = e.event.map((e) => ((e as num) * 100).toDouble()).toList();
samples = e.event.map((e) => ((e as num) * 100).toDouble()).toList();
});
}
});
}

void _stopVisualizer(AudioTrack track) async {
await widget.audioTrack.stopVisualizer();
_listener?.dispose();
await _listener?.dispose();
}

@override
Expand Down Expand Up @@ -105,4 +100,4 @@ class _SoundWaveformWidgetState extends State<SoundWaveformWidget>
},
);
}
}
}
19 changes: 13 additions & 6 deletions lib/src/track/local/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,26 @@ mixin VideoTrack on Track {

/// Used to group [LocalAudioTrack] and [RemoteAudioTrack].
mixin AudioTrack on Track {

EventChannel? _eventChannel ;
EventChannel? _eventChannel;
StreamSubscription? _streamSubscription;

@override
Future<void> onStarted() => startVisualizer();

@override
Future<void> onStopped() => stopVisualizer();

Future<void> startVisualizer() async {
if(_eventChannel != null) {
if (_eventChannel != null) {
return;
}

await Native.startVisualizer(mediaStreamTrack.id!);

_eventChannel = EventChannel('io.livekit.audio.visualizer/eventChannel-${mediaStreamTrack.id}');
_streamSubscription = _eventChannel?.receiveBroadcastStream().listen((event) {
_eventChannel = EventChannel(
'io.livekit.audio.visualizer/eventChannel-${mediaStreamTrack.id}');
_streamSubscription =
_eventChannel?.receiveBroadcastStream().listen((event) {
//logger.fine('[$objectId] visualizer event(${event})');
events.emit(AudioVisualizerEvent(
track: this,
Expand All @@ -82,7 +89,7 @@ mixin AudioTrack on Track {
}

Future<void> stopVisualizer() async {
if(_eventChannel == null) {
if (_eventChannel == null) {
return;
}
await Native.stopVisualizer(mediaStreamTrack.id!);
Expand Down
12 changes: 11 additions & 1 deletion lib/src/track/track.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ abstract class Track extends DisposableChangeNotifier
}

logger.fine('$objectId.start()');

startMonitor();

await onStarted();

_active = true;
return true;
}
Expand All @@ -125,6 +127,8 @@ abstract class Track extends DisposableChangeNotifier

stopMonitor();

await onStopped();

logger.fine('$objectId.stop()');

_active = false;
Expand Down Expand Up @@ -161,6 +165,12 @@ abstract class Track extends DisposableChangeNotifier
@internal
Future<bool> monitorStats();

@internal
Future<void> onStarted() async {}

@internal
Future<void> onStopped() async {}

@internal
void startMonitor() {
_monitorTimer ??= Timer.periodic(
Expand Down

0 comments on commit 6e1bad3

Please sign in to comment.