Skip to content

Commit 9ff54df

Browse files
committed
refactor : audio refactor
1 parent 6563b01 commit 9ff54df

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

lib/material/audios.dart

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import 'dart:io';
2+
13
import 'package:audioplayers/audio_cache.dart';
24
import 'package:audioplayers/audioplayers.dart';
5+
import 'package:flutter/material.dart';
36

47
Sounds sounds = Sounds._();
58

69
class Sounds {
7-
final _audioPlayer = AudioCache(fixedPlayer: AudioPlayer());
8-
9-
bool _initialized = false;
10-
11-
bool _playing = false;
10+
//the media file in application dir
11+
File _file;
1212

1313
bool muted = false;
1414

@@ -17,29 +17,24 @@ class Sounds {
1717
}
1818

1919
void _init() async {
20-
final player = _audioPlayer.fixedPlayer;
21-
player.setReleaseMode(ReleaseMode.STOP);
22-
player.audioPlayerStateChangeHandler = (state) {
23-
if (state == AudioPlayerState.PLAYING) {
24-
player.pause();
25-
_initialized = true;
26-
player.audioPlayerStateChangeHandler = null;
27-
}
28-
};
29-
await _audioPlayer.play("music.mp3");
20+
final audio = AudioCache();
21+
audio.load('music.mp3').then((file) {
22+
_file = file;
23+
debugPrint('file loaded : ${file.path}');
24+
}).catchError((e) {
25+
debugPrint('load music.mp3 failed : $e');
26+
});
3027
}
3128

3229
void _play(Duration start, Duration length) async {
33-
if (!_initialized || _playing || muted) {
30+
if (_file == null || muted) {
3431
return;
3532
}
36-
_playing = true;
37-
final player = _audioPlayer.fixedPlayer;
38-
await player.seek(start);
39-
await player.resume();
33+
final player = AudioPlayer();
34+
await player.play(_file.path, isLocal: true, position: start);
4035
await Future.delayed(length);
41-
player.pause();
42-
_playing = false;
36+
await player.stop();
37+
player.release();
4338
}
4439

4540
void start() {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies:
1818
flutter_localizations:
1919
sdk: flutter
2020
overlay_support: ^0.0.4
21-
audioplayers: ^0.8.0
21+
audioplayers: ^0.10.0
2222

2323
dev_dependencies:
2424
flutter_test:

0 commit comments

Comments
 (0)