Skip to content

Commit

Permalink
feat: seek player
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirJabbari committed Dec 5, 2023
1 parent 54c2b3a commit 2f3b9ee
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 37 deletions.
14 changes: 9 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:voice_message/pages/chat_page.dart';
import 'package:flutter/material.dart';
import 'package:sizer/sizer.dart';
import 'package:voice_message_package/voice_message_package.dart';
Expand All @@ -21,14 +20,19 @@ class MyApp extends StatelessWidget {
children: [
VoiceMessage(
controller: VoiceController(
id: '1',
audioSrc:
'https://dl.musichi.ir/1401/06/21/Ghors%202.mp3',
maxDuration: const Duration(seconds: 10),
isFile: false,
onComplete: (String id) {},
onPause: (String id) {},
onPlaying: (String id) {},
onComplete: () {
print('onComplete');
},
onPause: () {
print('onPause');
},
onPlaying: () {
print('onPlaying');
},
),
innerPadding: 12,
cornerRadius: 20,
Expand Down
7 changes: 3 additions & 4 deletions example/lib/widgets/bubble.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ class Bubble extends StatelessWidget {
Widget _bubble(BuildContext context) => voice
? VoiceMessage(
controller: VoiceController(
id: '1',
audioSrc:
// '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) {},
onPlaying: (String id) {},
onComplete: () {},
onPause: () {},
onPlaying: () {},
),
)
: Container(
Expand Down
17 changes: 17 additions & 0 deletions lib/src/helpers/play_status.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,21 @@ extension GetSpeed on PlaySpeed {
return 2.25;
}
}

String get playSpeedStr {
switch (this) {
case PlaySpeed.x1:
return "1.00x";
case PlaySpeed.x1_25:
return "1.25x";
case PlaySpeed.x1_5:
return "1.50x";
case PlaySpeed.x1_75:
return "1.75x";
case PlaySpeed.x2:
return "2.00x";
case PlaySpeed.x2_25:
return "2.25x";
}
}
}
32 changes: 6 additions & 26 deletions lib/src/voice_controller.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:ffi';
import 'dart:math';

import 'package:flutter/foundation.dart';
Expand All @@ -13,11 +12,10 @@ class VoiceController extends MyTicker {
final String audioSrc;
late Duration maxDuration;
Duration currentDuration = Duration.zero;
final Function(String id) onComplete;
final Function(String id) onPlaying;
final Function(String id) onPause;
final Function() onComplete;
final Function() onPlaying;
final Function() onPause;
final double noiseWidth = 50.5.w();
final String id;
late AnimationController animController;
final AudioPlayer _player = AudioPlayer();
final bool isFile;
Expand Down Expand Up @@ -53,7 +51,6 @@ class VoiceController extends MyTicker {
double get maxMillSeconds => maxDuration.inMilliseconds.toDouble();

VoiceController({
required this.id,
required this.audioSrc,
required this.maxDuration,
required this.isFile,
Expand Down Expand Up @@ -83,7 +80,7 @@ class VoiceController extends MyTicker {
playStatus = PlayStatus.downloading;
_updateUi();
await startPlaying(audioSrc);
onPlaying(id);
onPlaying();
} catch (err) {
playStatus = PlayStatus.downloadError;
_updateUi();
Expand All @@ -104,7 +101,7 @@ class VoiceController extends MyTicker {
playStatus = PlayStatus.init;
animController.reset();
_updateUi();
onComplete(id);
onComplete();
}
});
}
Expand Down Expand Up @@ -146,7 +143,7 @@ class VoiceController extends MyTicker {
_player.pause();
playStatus = PlayStatus.pause;
_updateUi();
onPause(id);
onPause();
}

void _listenToPlayerState() {
Expand All @@ -165,23 +162,6 @@ class VoiceController extends MyTicker {
});
}

String get playSpeedStr {
switch (speed) {
case PlaySpeed.x1:
return "1.00x";
case PlaySpeed.x1_25:
return "1.25x";
case PlaySpeed.x1_5:
return "1.50x";
case PlaySpeed.x1_75:
return "1.75x";
case PlaySpeed.x2:
return "2.00x";
case PlaySpeed.x2_25:
return "2.25x";
}
}

void changeSpeed() {
switch (speed) {
case PlaySpeed.x1:
Expand Down
8 changes: 6 additions & 2 deletions lib/src/voice_message.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:voice_message_package/src/helpers/play_status.dart';
import 'package:voice_message_package/src/helpers/utils.dart';
import 'package:voice_message_package/src/voice_controller.dart';
import 'package:voice_message_package/src/widgets/noises.dart';
Expand Down Expand Up @@ -111,6 +112,7 @@ class VoiceMessage extends StatelessWidget {
controller.onSeek(
Duration(milliseconds: value.toInt()),
);
controller.play();
},
),
),
Expand All @@ -135,8 +137,10 @@ class VoiceMessage extends StatelessWidget {
color: color,
borderRadius: BorderRadius.circular(4),
),
child:
Text(controller.playSpeedStr, style: circlesTextStyle),
child: Text(
controller.speed.playSpeedStr,
style: circlesTextStyle,
),
),
),
),
Expand Down

0 comments on commit 2f3b9ee

Please sign in to comment.