1
+ import 'dart:io' ;
2
+
1
3
import 'package:audioplayers/audio_cache.dart' ;
2
4
import 'package:audioplayers/audioplayers.dart' ;
5
+ import 'package:flutter/material.dart' ;
3
6
4
7
Sounds sounds = Sounds ._();
5
8
6
9
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;
12
12
13
13
bool muted = false ;
14
14
@@ -17,29 +17,24 @@ class Sounds {
17
17
}
18
18
19
19
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
+ });
30
27
}
31
28
32
29
void _play (Duration start, Duration length) async {
33
- if (! _initialized || _playing || muted) {
30
+ if (_file == null || muted) {
34
31
return ;
35
32
}
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);
40
35
await Future .delayed (length);
41
- player.pause ();
42
- _playing = false ;
36
+ await player.stop ();
37
+ player. release () ;
43
38
}
44
39
45
40
void start () {
0 commit comments