public interface AudioRecordingService
Service to record audio. The recording is saved in wav format in chunk files
of a given duration. The files are stored in the public storage, under a folder
that the developer will set.
The pattern for these files name will be
audioFile-###-yyyy-MM-dd.HH-mm-ss.SSS,wav
, where ### is the number of
chunk, starting from 000.
Note that given the limited space available on the device, it is convenient to remove the content either before starting a new recording or when the files have been extracted.
Dependencies
Example
The following code snippet shows how to start recording 10 minutes of audio in 10 files of 1 minute duration each.
Services.get(AudioRecordingService.class).ifPresent(audio -> {
audio.clearAudioFolder();
audio.getAudioChunkFiles().addListener((ListChangeListener.Change<? extends String> c) -> {
while (c.next()) {
if (c.wasAdded()) {
for (String fileName : c.getAddedSubList()) {
File audioFile = new File(audio.getAudioFolder(), fileName);
System.out.println("New audio chunk: " + audioFile.getAbsolutePath());
}
}
if (audio.getAudioChunkFiles().size() == 10) {
audio.stopRecording();
}
}
});
audio.startRecording(44100f, 16, 2, 60);
});
Android Configuration
The following permission
and activity need to be added to the
android manifest configuration file:
<manifest ...>
...
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application ...>
...
<activity android:name="com.gluonhq.impl.charm.down.plugins.android.PermissionRequestActivity" />
</application>
</manifest>
iOS Configuration
The following keys are required:
<key>NSMicrophoneUsageDescription</key>
<string>Need microphone access for recording audio</string>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
Note: to get access to the public storage from iTunes these keys can be added
as well:
<key>CFBundleDisplayName</key>
<string>$ApplicationName</string>
<key>UIFileSharingEnabled</key>
<string>YES</string>
Type | Property and Description |
---|---|
javafx.beans.property.ReadOnlyBooleanProperty |
recording
Returns
true when the audio recording is currently active and false
if audio recording is stopped. |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_EXTERNAL_FOLDER |
Modifier and Type | Method and Description |
---|---|
void |
clearAudioFolder()
Removes the content of the audio folder.
|
javafx.beans.property.ReadOnlyListProperty<java.lang.String> |
getAudioChunkFiles()
Returns a read only observable list of file names.
|
java.io.File |
getAudioFolder()
Returns the folder where all the audio files will be stored
|
javafx.beans.property.ReadOnlyBooleanProperty |
recordingProperty()
Returns
true when the audio recording is currently active and false
if audio recording is stopped. |
void |
setAudioFolderName(java.lang.String folderName)
Sets the name of the external folder where the audio files will be saved.
|
void |
startRecording(float sampleRate,
int sampleSizeInBits,
int channels,
int chunkRecordTime)
Start audio recording with the given parameters.
|
void |
stopRecording()
Stop audio recording.
|
javafx.beans.property.ReadOnlyBooleanProperty recordingProperty
true
when the audio recording is currently active and false
if audio recording is stopped.static final java.lang.String DEFAULT_EXTERNAL_FOLDER
void setAudioFolderName(java.lang.String folderName)
DEFAULT_EXTERNAL_FOLDER
.folderName
- the name of the external folder where the audio files will
be savedjava.io.File getAudioFolder()
void clearAudioFolder()
This method can be called at any time. It is convenient to call it before the audio recording starts, or when it ends and the audio files have been safely extracted.
void startRecording(float sampleRate, int sampleSizeInBits, int channels, int chunkRecordTime)
sampleRate
- the number of samples per second (8000.0f, 44100.0f, ...)sampleSizeInBits
- the number of bits in each sample (16 or 8)channels
- the number of channels (1 for mono, 2 for stereo)chunkRecordTime
- the duration in seconds of each chunk (60, 360, ...)void stopRecording()
javafx.beans.property.ReadOnlyBooleanProperty recordingProperty()
true
when the audio recording is currently active and false
if audio recording is stopped.javafx.beans.property.ReadOnlyListProperty<java.lang.String> getAudioChunkFiles()
audio folder
. It can be used during recording
to track when new audio chunks are made available in the audio folder.ReadOnlyListProperty
of file names