public final class Sound
extends java.lang.Object
Playing beeps is supported under all platforms but tones are only supported where the underlying platform supports generating tones. Tones aren't supported under Java.
Here is an example that beeps the speaker and plays a tone:
Sound.beep(); Sound.tone(4000, 300);
Modifier and Type | Method and Description |
---|---|
static void |
beep()
Plays the device's default beep sound.
|
static void |
fromText(java.lang.String text)
Activates speech and let the user hear the given text.
|
static void |
midiTone(int midiNoteNumber,
int duration)
Deprecated.
Since it only works on Windows 32.
|
static void |
play(java.lang.String filename)
Plays the given short wav or mp3 file.
|
static void |
setEnabled(boolean on)
Sets the sound of the device on/off.
|
static void |
tone(int freq,
int duration)
Plays a tone of the specified frequency for the specified
duration.
|
static java.lang.String |
toText(java.lang.String params)
Activates speech and let the user dictate something that will be returned.
|
public static void beep()
public static void tone(int freq, int duration)
freq
- frequency in hertz from 32 to 32767duration
- duration in millisecondspublic static void setEnabled(boolean on)
Important: If the user had set its device sound to off, this method will not turn it on, ie, it will keep the device in silence. Otherwise, it will set the volume to its original configuration.
on
- if true enables the sound, if false disable it.@Deprecated public static void midiTone(int midiNoteNumber, int duration)
Note: The smaller the midi number, the greater the error in the frequency, due to the fact that the frequency calculated must be an integer.
midiNoteNumber
- number of the note according to MIDI standard
(eg. A (440hz) = 69), from 24 to 143duration
- duration in millisecondspublic static void play(java.lang.String filename)
new File("device/mysound.mp3", File.CREATE_EMPTY).writeAndClose(Vm.getFile("mysound.mp3"));Then you play it using:
Sound.play("device/mysound.mp3");The last sound is cached, so playing it again is fast. If you want to unload it, just call
Sound.play("");
,
but this is not needed since small mp3/wav files consumes just a few memory. Cache is not done in Win32.public static java.lang.String toText(java.lang.String params)
title:title to show to user timeout: timeout in millis that will be waiten to return after dictation finishesIf you pass more than one parameter, separate them with | (pipe). So, for example:
String ret = Sound.toText("title:Answer|timeout:500");
public static void fromText(java.lang.String text)
languages: shows in adb's logcat the available languages. For example, in the Samsung S4 mini, it shows: [spa_MEX_f00, tur_TUR_l02, jpn_JPN_f00, kor_KOR_f00, por_PRT_f00, eng_USA_f00, rus_RUS_f00, eng_GBR_f00, por_BRA_f00, fra_FRA_f00, por_BRA_l01, ita_ITA_f00, deu_DEU_f00, zho_CHN_f00, spa_ESP_f00] locale: the locale to be used. You can pass, for example, spa_ESP or spa_ESP_f00 speech: the speech speed. The standard is 1.0. A lower value will decrease the speed, a higher value will increase it. quit: quits the speech engine.Note that the method call blocks until the text dictation finishes. This is a sample to use Brazilian portuguese:
Sound.fromText("locale:por_BRA,speech:0.75"); Sound.fromText("você é homem ou mulher?"); String ret = Sound.toText("title:Responda|timeout:500"); lab.setText("Answer: "+ret);