-
Notifications
You must be signed in to change notification settings - Fork 18
/
GameSoundService.java
121 lines (99 loc) · 4.34 KB
/
GameSoundService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package org.hit.android.haim.texasholdem.view;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import org.hit.android.haim.texasholdem.R;
import org.hit.android.haim.texasholdem.common.model.bean.game.Player;
import org.hit.android.haim.texasholdem.common.model.game.GameEngine;
import org.hit.android.haim.texasholdem.model.game.Game;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Play game sound effects in background
* @author Haim Adrian
* @since 12-Jun-21
*/
public class GameSoundService extends Service implements Game.GameListener {
private static final String LOGGER = GameSoundService.class.getSimpleName();
/**
* Map between {@link org.hit.android.haim.texasholdem.model.game.Game.GameStepType} to its corresponding
* media player, so we can play sound effects for each game step.
*/
private Map<Game.GameStepType, MediaPlayer> mediaPlayers;
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(LOGGER, "onBind");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(LOGGER, "onStartCommand");
MediaPlayer chipsLongMediaPlayer = MediaPlayer.create(this, R.raw.chips_long);
mediaPlayers = new HashMap<>(Game.GameStepType.values().length);
mediaPlayers.put(Game.GameStepType.CALL, MediaPlayer.create(this, R.raw.chips_short));
mediaPlayers.put(Game.GameStepType.RAISE, chipsLongMediaPlayer);
mediaPlayers.put(Game.GameStepType.ALL_IN, chipsLongMediaPlayer);
mediaPlayers.put(Game.GameStepType.TIMER, MediaPlayer.create(this, R.raw.kitchen_timer));
mediaPlayers.put(Game.GameStepType.CHECK, MediaPlayer.create(this, R.raw.knock_on_door));
mediaPlayers.put(Game.GameStepType.DEAL_CARD, MediaPlayer.create(this, R.raw.dealing_card));
mediaPlayers.put(Game.GameStepType.FLIP_CARD, MediaPlayer.create(this, R.raw.flip_card));
mediaPlayers.put(Game.GameStepType.WIN, MediaPlayer.create(this, R.raw.success));
mediaPlayers.put(Game.GameStepType.LOSE, MediaPlayer.create(this, R.raw.fail));
mediaPlayers.put(Game.GameStepType.FOLD, MediaPlayer.create(this, R.raw.dealing_card));
// Listen to game steps so we can play sound effects for each step
Game.getInstance().addGameListener(this);
// Use sticky so onStartCommand will be called again in case service is killed.
return Service.START_STICKY;
}
@Override
public void onDestroy() {
Log.d(LOGGER, "onDestroy");
Game.getInstance().removeGameStepListener(this);
// Stop game to shutdown background threads
try { Game.getInstance().stop(null); } catch (Exception ignore) { }
if (mediaPlayers != null) {
mediaPlayers.values().forEach(GameSoundService::releaseMediaPlayer);
mediaPlayers.clear();
}
stopSelf();
super.onDestroy();
}
private static void releaseMediaPlayer(MediaPlayer mediaPlayer) {
try { mediaPlayer.stop(); } catch (Exception ignore) {}
try { mediaPlayer.reset(); } catch (Exception ignore) {} // Reset media player before releasing it so we will not get "mediaplayer went away with unhandled events" warning
try { mediaPlayer.release(); } catch (Exception ignore) {}
}
private static void restartMediaPlayer(MediaPlayer mediaPlayer) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.start();
}
@Override
public void onStep(GameEngine gameEngine, Game.GameStepType step) {
if (step != null) {
Log.d(LOGGER, "onStep: " + step);
MediaPlayer mediaPlayer = mediaPlayers.get(step);
if (mediaPlayer != null) {
restartMediaPlayer(mediaPlayer);
}
}
}
@Override
public void refresh(GameEngine gameEngine) {
// Do nothing
}
@Override
public void playersRefresh(Set<Player> players) {
// Do nothing
}
@Override
public void onGameError(String errorMessage) {
// Do nothing
}
}