speaker_wave.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import 'dart:async';
  2. import 'dart:math';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_sound/flutter_sound.dart';
  5. /// 扬声器输出等级可视化
  6. class SpeakerWave extends StatefulWidget {
  7. final FlutterSoundPlayer audioPlayer;
  8. final double width;
  9. final double height;
  10. final Color color;
  11. final bool isPlaying;
  12. SpeakerWave({
  13. required this.audioPlayer,
  14. this.isPlaying = false,
  15. this.width = 250.0,
  16. this.height = 40.0,
  17. this.color = Colors.blue,
  18. });
  19. @override
  20. _SpeakerWaveState createState() => _SpeakerWaveState();
  21. }
  22. class _SpeakerWaveState extends State<SpeakerWave> {
  23. StreamSubscription? _waveformSubscription;
  24. int _randomWaveform = 0;
  25. static const _itemHeight = 10.0;
  26. static const _itemCount = 5;
  27. @override
  28. void initState() {
  29. super.initState();
  30. print("监听扬声器输出:${widget.audioPlayer}");
  31. /// 监听扬声器输出
  32. _waveformSubscription =
  33. widget.audioPlayer.onProgress?.listen((PlaybackDisposition data) {
  34. setState(() {
  35. _randomWaveform = Random().nextInt(_itemCount);
  36. });
  37. if (data.duration - data.position < Duration(milliseconds: 500)) {
  38. setState(() {
  39. _randomWaveform = 0;
  40. });
  41. }
  42. });
  43. }
  44. @override
  45. void dispose() {
  46. super.dispose();
  47. _waveformSubscription?.cancel();
  48. _waveformSubscription = null;
  49. }
  50. @override
  51. Widget build(BuildContext context) {
  52. return Container(
  53. width: widget.width,
  54. height: widget.height,
  55. child: Row(
  56. crossAxisAlignment: CrossAxisAlignment.center,
  57. children: [
  58. Text(
  59. "扬声器输出:",
  60. style: TextStyle(
  61. height: 1,
  62. color: Colors.black87,
  63. ),
  64. ),
  65. if (widget.isPlaying) ...[
  66. for (var i = 0; i < _randomWaveform; i++)
  67. Expanded(
  68. child: buildActiveItem(),
  69. ),
  70. for (var i = 0; i < _itemCount - _randomWaveform; i++)
  71. Expanded(
  72. child: buildInactiveItem(),
  73. ),
  74. ] else ...[
  75. for (var i = 0; i < _itemCount; i++)
  76. Expanded(
  77. child: buildInactiveItem(),
  78. ),
  79. ]
  80. ],
  81. ),
  82. );
  83. }
  84. Widget buildActiveItem() {
  85. return Container(
  86. height: _itemHeight,
  87. margin: EdgeInsets.symmetric(horizontal: 3),
  88. decoration: BoxDecoration(
  89. borderRadius: BorderRadius.all(Radius.circular(5)),
  90. color: widget.color,
  91. ),
  92. );
  93. }
  94. Widget buildInactiveItem() {
  95. return Container(
  96. height: _itemHeight,
  97. margin: EdgeInsets.symmetric(horizontal: 3),
  98. decoration: BoxDecoration(
  99. borderRadius: BorderRadius.all(Radius.circular(5)),
  100. color: Colors.grey[300],
  101. ),
  102. );
  103. }
  104. }