Interface AudioRecordingService
- All Known Implementing Classes:
DefaultAudioRecordingService
,DesktopAudioRecordingService
,DummyAudioRecordingService
,IOSAudioRecordingService
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.
AudioRecordingService.create().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);
});
Requirements
The service requires the following changes on Android and iOS.
However, these are handled automatically by the GluonFX plugin, when used.
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.attach.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>
- Since:
- 3.5.0
-
Property Summary
TypePropertyDescriptionjavafx.beans.property.ReadOnlyBooleanProperty
Returnstrue
when the audio recording is currently active andfalse
if audio recording is stopped. -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Removes the content of the audio folder.static Optional<AudioRecordingService>
create()
Returns an instance ofAudioRecordingService
.javafx.beans.property.ReadOnlyListProperty<String>
Returns a read only observable list of file names.Returns the folder where all the audio files will be storedjavafx.beans.property.ReadOnlyBooleanProperty
Returnstrue
when the audio recording is currently active andfalse
if audio recording is stopped.void
setAudioFolderName
(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
Stop audio recording.
-
Property Details
-
recording
javafx.beans.property.ReadOnlyBooleanProperty recordingPropertyReturnstrue
when the audio recording is currently active andfalse
if audio recording is stopped.
-
-
Field Details
-
DEFAULT_EXTERNAL_FOLDER
- See Also:
-
-
Method Details
-
create
Returns an instance ofAudioRecordingService
.- Returns:
- An instance of
AudioRecordingService
.
-
setAudioFolderName
Sets the name of the external folder where the audio files will be saved. If not set, by default it will useDEFAULT_EXTERNAL_FOLDER
.- Parameters:
folderName
- the name of the external folder where the audio files will be saved
-
getAudioFolder
File getAudioFolder()Returns the folder where all the audio files will be stored- Returns:
- a File with the audio folder
-
clearAudioFolder
void clearAudioFolder()Removes the content of the audio folder.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.
-
startRecording
void startRecording(float sampleRate, int sampleSizeInBits, int channels, int chunkRecordTime) Start audio recording with the given parameters. The recorded audio files are saved in wav format in chunks of the specified recording time.- Parameters:
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, ...)
-
stopRecording
void stopRecording()Stop audio recording. -
recordingProperty
javafx.beans.property.ReadOnlyBooleanProperty recordingProperty()Returnstrue
when the audio recording is currently active andfalse
if audio recording is stopped. -
getAudioChunkFiles
javafx.beans.property.ReadOnlyListProperty<String> getAudioChunkFiles()Returns a read only observable list of file names. It contains a list of file chunks that are saved in theaudio folder
. It can be used during recording to track when new audio chunks are made available in the audio folder.- Returns:
- A
ReadOnlyListProperty
of file names
-