From 6e1bad309adcd01cb3e0c3a8aa6c7a8265e3ff5a Mon Sep 17 00:00:00 2001 From: duan Date: Fri, 29 Nov 2024 21:29:48 +0800 Subject: [PATCH] update. --- example/lib/widgets/sound_waveform.dart | 19 +++++++------------ lib/src/track/local/local.dart | 19 +++++++++++++------ lib/src/track/track.dart | 12 +++++++++++- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/example/lib/widgets/sound_waveform.dart b/example/lib/widgets/sound_waveform.dart index 3d36bb46b..9a37942cd 100644 --- a/example/lib/widgets/sound_waveform.dart +++ b/example/lib/widgets/sound_waveform.dart @@ -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; @@ -27,25 +24,23 @@ class SoundWaveformWidget extends StatefulWidget { class _SoundWaveformWidgetState extends State with TickerProviderStateMixin { late AnimationController controller; - List samples = [0,0,0,0,0,0,0]; - EventsListener? _listener; + List samples = [0, 0, 0, 0, 0, 0, 0]; + EventsListener? _listener; void _startVisualizer(AudioTrack track) async { - await widget.audioTrack.startVisualizer(); - _listener?.dispose(); + await _listener?.dispose(); _listener = track.createListener(); _listener?.on((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 @@ -105,4 +100,4 @@ class _SoundWaveformWidgetState extends State }, ); } -} \ No newline at end of file +} diff --git a/lib/src/track/local/local.dart b/lib/src/track/local/local.dart index 625e0e798..9d4ce20d8 100644 --- a/lib/src/track/local/local.dart +++ b/lib/src/track/local/local.dart @@ -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 onStarted() => startVisualizer(); + + @override + Future onStopped() => stopVisualizer(); + Future 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, @@ -82,7 +89,7 @@ mixin AudioTrack on Track { } Future stopVisualizer() async { - if(_eventChannel == null) { + if (_eventChannel == null) { return; } await Native.stopVisualizer(mediaStreamTrack.id!); diff --git a/lib/src/track/track.dart b/lib/src/track/track.dart index 2f77e5973..3dcccee98 100644 --- a/lib/src/track/track.dart +++ b/lib/src/track/track.dart @@ -107,9 +107,11 @@ abstract class Track extends DisposableChangeNotifier } logger.fine('$objectId.start()'); - + startMonitor(); + await onStarted(); + _active = true; return true; } @@ -125,6 +127,8 @@ abstract class Track extends DisposableChangeNotifier stopMonitor(); + await onStopped(); + logger.fine('$objectId.stop()'); _active = false; @@ -161,6 +165,12 @@ abstract class Track extends DisposableChangeNotifier @internal Future monitorStats(); + @internal + Future onStarted() async {} + + @internal + Future onStopped() async {} + @internal void startMonitor() { _monitorTimer ??= Timer.periodic(