-
Notifications
You must be signed in to change notification settings - Fork 545
/
Copy pathAudioPlayer.java
142 lines (126 loc) · 3.37 KB
/
AudioPlayer.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Created by Matthias Mueller - Intel Intelligent Systems Lab - 2020
package org.openbot.env;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.speech.tts.TextToSpeech;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import org.openbot.utils.Enums;
// Convert text to speech
// https://ttsmp3.com
public class AudioPlayer {
private MediaPlayer mp;
private final Context context;
private TextToSpeech tts;
public AudioPlayer(Context context) {
mp = new MediaPlayer();
this.context = context;
tts =
new TextToSpeech(
context,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
tts.setSpeechRate(1.0f);
tts.setPitch(0.5f);
}
}
});
}
// Play audio from string id.
public void playFromStringID(int id) {
playFromString(context.getString(id));
}
// Play audio from string.
public void playFromString(String message) {
tts.speak(
message
.replace("'Y'", "why")
.replace("'B'", "bee")
.replace("'X'", "axe")
.replace("AR Core", "ay are core"),
TextToSpeech.QUEUE_FLUSH,
null,
"ttsPlayer");
}
// Play from a resource file
public void play(int id) {
try {
mp.reset();
mp = MediaPlayer.create(context, id);
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
// Play from a file in storage
public void play(String path) {
try {
mp.reset();
mp.setDataSource(path);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
// Play from media asset
public void play(String assetFolder, String fileName) {
try {
AssetFileDescriptor afd =
context
.getAssets()
.openFd("media" + File.separator + assetFolder + File.separator + fileName);
mp.reset();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.prepare();
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void playDriveMode(String voice, Enums.DriveMode driveMode) {
switch (driveMode) {
case DUAL:
play(voice, "dual_drive_control.mp3");
break;
case GAME:
play(voice, "video_game_control.mp3");
break;
case JOYSTICK:
play(voice, "joystick_control.mp3");
break;
}
}
public void playSpeedMode(String voice, Enums.SpeedMode speedMode) {
switch (speedMode) {
case SLOW:
play(voice, "slow_speed.mp3");
break;
case NORMAL:
play(voice, "normal_speed.mp3");
break;
case FAST:
play(voice, "fast_speed.mp3");
break;
}
}
public void playNoise(String voice, boolean isEnabled) {
if (isEnabled) {
play(voice, "noise_enabled.mp3");
} else {
play(voice, "noise_disabled.mp3");
}
}
public void playLogging(String voice, boolean isEnabled) {
if (isEnabled) {
play(voice, "logging_started.mp3");
} else {
play(voice, "logging_stopped.mp3");
}
}
}