Skip to content

Commit

Permalink
update audio controller
Browse files Browse the repository at this point in the history
  • Loading branch information
mehranshoqi committed Dec 5, 2023
2 parents 288e178 + 418d527 commit 54c2b3a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 23 deletions.
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class MyApp extends StatelessWidget {
controller: VoiceController(
id: '1',
audioSrc:
'https://dl.musicdel.ir/Music/1400/08/morteza_pashaei_setayesh%20128.mp3',
maxDuration: const Duration(seconds: 30),
'https://dl.musichi.ir/1401/06/21/Ghors%202.mp3',
maxDuration: const Duration(seconds: 10),
isFile: false,
onComplete: (String id) {},
onPause: (String id) {},
Expand Down
5 changes: 3 additions & 2 deletions example/lib/widgets/bubble.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ class Bubble extends StatelessWidget {
controller: VoiceController(
id: '1',
audioSrc:
'https://dl.musicdel.ir/Music/1400/08/morteza_pashaei_setayesh%20128.mp3',
maxDuration: const Duration(seconds: 30),
// 'https://dl.musicdel.ir/Music/1400/08/morteza_pashaei_setayesh%20128.mp3',
'https://dl.musichi.ir/1401/06/21/Ghors%202.mp3',
maxDuration: const Duration(seconds: 0),
isFile: false,
onComplete: (String id) {},
onPause: (String id) {},
Expand Down
35 changes: 20 additions & 15 deletions lib/src/voice_controller.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:ffi';
import 'dart:math';

import 'package:flutter/foundation.dart';
Expand All @@ -23,7 +24,7 @@ class VoiceController extends MyTicker {
PlayStatus playStatus = PlayStatus.init;
PlaySpeed speed = PlaySpeed.x1;
ValueNotifier updater = ValueNotifier(null);
final randoms = <double>[];
List<double>? randoms;
StreamSubscription? positionStream;
StreamSubscription? playerStateStream;

Expand Down Expand Up @@ -59,27 +60,29 @@ class VoiceController extends MyTicker {
required this.onComplete,
required this.onPause,
required this.onPlaying,
this.randoms,
}) {
_setRandoms();
if (randoms?.isEmpty ?? true) _setRandoms();
animController = AnimationController(
vsync: this,
upperBound: noiseWidth,
duration: maxDuration,
);
init();
_listenToRemindingTime();
_listenToPlayerState();
// animController.addListener(() {
// print("value is "+animController.value.toString());
// });
}

Future initAndPlay() async {
playStatus = PlayStatus.downloading;
Future init() async {
await setMaxDuration(audioSrc);
_updateUi();
}

Future play() async {
try {
await setMaxDuration(audioSrc);
playStatus = PlayStatus.downloading;
_updateUi();
await startPlaying(audioSrc);

onPlaying(id);
} catch (err) {
playStatus = PlayStatus.downloadError;
Expand All @@ -90,7 +93,8 @@ class VoiceController extends MyTicker {

void _listenToRemindingTime() {
positionStream = _player.positionStream.listen((Duration p) async {
currentDuration = p;
if (!isDownloading) currentDuration = p;

final value = (noiseWidth * currentMillSeconds) / maxMillSeconds;
animController.value = value;
_updateUi();
Expand Down Expand Up @@ -209,8 +213,9 @@ class VoiceController extends MyTicker {
}

void _setRandoms() {
for (var i = 0; i < 50; i++) {
randoms.add(5.74.w() * Random().nextDouble() + .26.w());
randoms = [];
for (var i = 0; i < 44; i++) {
randoms!.add(5.74.w() * Random().nextDouble() + .26.w());
}
}

Expand All @@ -225,10 +230,10 @@ class VoiceController extends MyTicker {
if (currentDuration == Duration.zero) {
return maxDuration.formattedTime;
}
if (isSeeking) {
if (isSeeking || isPause) {
return currentDuration.formattedTime;
}
if (isPause || isInit) {
if (isInit) {
return maxDuration.formattedTime;
}
return currentDuration.formattedTime;
Expand All @@ -244,7 +249,7 @@ class VoiceController extends MyTicker {
}
} catch (err) {
if (kDebugMode) {
print("cant get the max duration from the path $path");
debugPrint("cant get the max duration from the path $path");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/voice_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class VoiceMessage extends StatelessWidget {
alignment: Alignment.center,
children: [
Noises(
rList: controller.randoms,
rList: controller.randoms!,
activeSliderColor: activeSliderColor,
),
AnimatedBuilder(
Expand Down
14 changes: 14 additions & 0 deletions lib/src/widgets/loading_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:flutter/material.dart';

class LoadingWidget extends StatelessWidget {
const LoadingWidget({super.key});

@override
Widget build(BuildContext context) => const Padding(
padding: EdgeInsets.all(8),
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
);
}
7 changes: 4 additions & 3 deletions lib/src/widgets/play_pause_button.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:voice_message_package/src/widgets/loading_widget.dart';
import 'package:voice_message_package/voice_message_package.dart';

class PlayPauseButton extends StatelessWidget {
Expand All @@ -17,16 +18,16 @@ class PlayPauseButton extends StatelessWidget {
@override
Widget build(BuildContext context) => InkWell(
onTap: controller.isDownloadError
? controller.initAndPlay
? controller.play
: controller.isPlaying
? controller.pausePlaying
: controller.initAndPlay,
: controller.play,
child: Container(
height: size,
width: size,
decoration: BoxDecoration(color: color, shape: BoxShape.circle),
child: controller.isDownloading
? const CupertinoActivityIndicator()
? const LoadingWidget()
: Icon(
controller.isDownloadError
? Icons.refresh
Expand Down

0 comments on commit 54c2b3a

Please sign in to comment.