Skip to content

Commit f9bf9b7

Browse files
authored
Merge pull request boyan01#2 from boyan01/dev
sounds fix
2 parents 5b85a51 + 24447a3 commit f9bf9b7

File tree

11 files changed

+76
-57
lines changed

11 files changed

+76
-57
lines changed

assets/audios/clean.mp3

8.13 KB
Binary file not shown.

assets/audios/drop.mp3

6.25 KB
Binary file not shown.

assets/audios/explosion.mp3

11.8 KB
Binary file not shown.

assets/audios/move.mp3

5.36 KB
Binary file not shown.

assets/audios/rotate.mp3

5.48 KB
Binary file not shown.

assets/audios/start.mp3

27.5 KB
Binary file not shown.

assets/music.mp3

-53.1 KB
Binary file not shown.

lib/gamer/gamer.dart

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,14 @@ class GameControl extends State<Game> with RouteAware {
131131
return next;
132132
}
133133

134+
SoundState get _sound => Sound.of(context);
135+
134136
void rotate() {
135137
if (_states == GameStates.running && _current != null) {
136138
final next = _current.rotate();
137139
if (next.isValidInMatrix(_data)) {
138140
_current = next;
139-
sounds.rotate();
141+
_sound.rotate();
140142
}
141143
}
142144
setState(() {});
@@ -149,7 +151,7 @@ class GameControl extends State<Game> with RouteAware {
149151
final next = _current.right();
150152
if (next.isValidInMatrix(_data)) {
151153
_current = next;
152-
sounds.move();
154+
_sound.move();
153155
}
154156
}
155157
setState(() {});
@@ -162,7 +164,7 @@ class GameControl extends State<Game> with RouteAware {
162164
final next = _current.left();
163165
if (next.isValidInMatrix(_data)) {
164166
_current = next;
165-
sounds.move();
167+
_sound.move();
166168
}
167169
}
168170
setState(() {});
@@ -177,7 +179,7 @@ class GameControl extends State<Game> with RouteAware {
177179
_states = GameStates.drop;
178180
setState(() {});
179181
await Future.delayed(const Duration(milliseconds: 100));
180-
_mixCurrentIntoData(mixSound: sounds.fall);
182+
_mixCurrentIntoData(mixSound: _sound.fall);
181183
break;
182184
}
183185
}
@@ -193,7 +195,7 @@ class GameControl extends State<Game> with RouteAware {
193195
if (next.isValidInMatrix(_data)) {
194196
_current = next;
195197
if (enableSounds) {
196-
sounds.move();
198+
_sound.move();
197199
}
198200
} else {
199201
_mixCurrentIntoData();
@@ -225,7 +227,7 @@ class GameControl extends State<Game> with RouteAware {
225227
if (clearLines.isNotEmpty) {
226228
setState(() => _states = GameStates.clear);
227229

228-
sounds.clear();
230+
_sound.clear();
229231

230232
///消除效果动画
231233
for (int count = 0; count < 5; count++) {
@@ -325,7 +327,7 @@ class GameControl extends State<Game> with RouteAware {
325327
if (_states == GameStates.reset) {
326328
return;
327329
}
328-
sounds.start();
330+
_sound.start();
329331
_states = GameStates.reset;
330332
() async {
331333
int line = GAME_PAD_MATRIX_H;
@@ -383,13 +385,13 @@ class GameControl extends State<Game> with RouteAware {
383385
}
384386
debugPrint("game states : $_states");
385387
return GameState(
386-
mixed, _states, _level, sounds.muted, _points, _cleared, _next,
388+
mixed, _states, _level, _sound.mute, _points, _cleared, _next,
387389
child: widget.child);
388390
}
389391

390392
void soundSwitch() {
391393
setState(() {
392-
sounds.muted = !sounds.muted;
394+
_sound.mute = !_sound.mute;
393395
});
394396
}
395397
}

lib/main.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
22
import 'package:flutter_localizations/flutter_localizations.dart';
33
import 'package:tetris/gamer/gamer.dart';
44
import 'package:tetris/generated/i18n.dart';
5+
import 'package:tetris/material/audios.dart';
56
import 'package:tetris/panel/page_portrait.dart';
67

78
void main() {
@@ -41,7 +42,7 @@ class MyApp extends StatelessWidget {
4142
primarySwatch: Colors.blue,
4243
),
4344
home: Scaffold(
44-
body: Game(child: _HomePage()),
45+
body: Sound(child: Game(child: _HomePage())),
4546
),
4647
);
4748
}

lib/material/audios.dart

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,88 @@
1-
import 'package:audioplayers/audio_cache.dart';
2-
import 'package:audioplayers/audioplayers.dart';
1+
import 'dart:async';
32

4-
Sounds sounds = Sounds._();
3+
import 'package:flutter/material.dart';
4+
import 'package:flutter/services.dart';
5+
import 'package:soundpool/soundpool.dart';
56

6-
class Sounds {
7-
final _audioPlayer = AudioCache(fixedPlayer: AudioPlayer());
7+
class Sound extends StatefulWidget {
8+
final Widget child;
89

9-
bool _initialized = false;
10+
const Sound({Key key, this.child}) : super(key: key);
1011

11-
bool _playing = false;
12+
@override
13+
SoundState createState() => SoundState();
1214

13-
bool muted = false;
14-
15-
Sounds._() {
16-
_init();
15+
static SoundState of(BuildContext context) {
16+
final state = context.ancestorStateOfType(const TypeMatcher<SoundState>());
17+
assert(state != null, 'can not find Sound widget');
18+
return state;
1719
}
20+
}
21+
22+
const _SOUNDS = [
23+
'clean.mp3',
24+
'drop.mp3',
25+
'explosion.mp3',
26+
'move.mp3',
27+
'rotate.mp3',
28+
'start.mp3'
29+
];
30+
31+
class SoundState extends State<Sound> {
32+
Soundpool _pool;
1833

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");
34+
Map<String, int> _soundIds;
35+
36+
bool mute = false;
37+
38+
void _play(String name) {
39+
final soundId = _soundIds[name];
40+
if (soundId != null && !mute) {
41+
_pool.play(soundId);
42+
}
3043
}
3144

32-
void _play(Duration start, Duration length) async {
33-
if (!_initialized || _playing || muted) {
34-
return;
45+
@override
46+
void initState() {
47+
super.initState();
48+
_pool = Soundpool(streamType: StreamType.music);
49+
_soundIds = Map();
50+
for (var value in _SOUNDS) {
51+
scheduleMicrotask(() async {
52+
final data = await rootBundle.load('assets/audios/$value');
53+
_soundIds[value] = await _pool.load(data);
54+
});
3555
}
36-
_playing = true;
37-
final player = _audioPlayer.fixedPlayer;
38-
await player.seek(start);
39-
await player.resume();
40-
await Future.delayed(length);
41-
player.pause();
42-
_playing = false;
56+
}
57+
58+
@override
59+
void dispose() {
60+
super.dispose();
61+
_pool.dispose();
62+
}
63+
64+
@override
65+
Widget build(BuildContext context) {
66+
return widget.child;
4367
}
4468

4569
void start() {
46-
_play(
47-
const Duration(milliseconds: 3720), const Duration(milliseconds: 3622));
70+
_play('start.mp3');
4871
}
4972

5073
void clear() {
51-
_play(const Duration(milliseconds: 0), const Duration(milliseconds: 767));
74+
_play('clean.mp3');
5275
}
5376

5477
void fall() {
55-
_play(
56-
const Duration(milliseconds: 1255), const Duration(milliseconds: 354));
57-
}
58-
59-
void gameOver() {
60-
_play(
61-
const Duration(milliseconds: 8127), const Duration(milliseconds: 1143));
78+
_play('drop.mp3');
6279
}
6380

6481
void rotate() {
65-
_play(const Duration(milliseconds: 2247), const Duration(milliseconds: 80));
82+
_play('rotate.mp3');
6683
}
6784

6885
void move() {
69-
_play(
70-
const Duration(milliseconds: 2908), const Duration(milliseconds: 143));
86+
_play('move.mp3');
7187
}
7288
}

pubspec.yaml

Lines changed: 2 additions & 2 deletions
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+
soundpool: ^0.4.1
2222

2323
dev_dependencies:
2424
flutter_test:
@@ -34,7 +34,7 @@ flutter:
3434

3535
assets:
3636
- assets/material.png
37-
- assets/music.mp3
37+
- assets/audios/
3838
- assets/alipay.jpg
3939
- assets/wechat.png
4040

0 commit comments

Comments
 (0)