Interface AudioRecordingService
-
- All Known Implementing Classes:
DefaultAudioRecordingService
,DesktopAudioRecordingService
,DummyAudioRecordingService
,IOSAudioRecordingService
public interface AudioRecordingService
AudioRecording Service.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>
<key>CFBundleDisplayName</key> <string>$ApplicationName</string> <key>UIFileSharingEnabled</key> <string>YES</string>
- Since:
- 3.5.0
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_EXTERNAL_FOLDER
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description void
clearAudioFolder()
Removes the content of the audio folder.static java.util.Optional<AudioRecordingService>
create()
Returns an instance ofAudioRecordingService
.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 storedjavafx.beans.property.ReadOnlyBooleanProperty
recordingProperty()
Returnstrue
when the audio recording is currently active andfalse
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.
-
-
-
Field Detail
-
DEFAULT_EXTERNAL_FOLDER
static final java.lang.String DEFAULT_EXTERNAL_FOLDER
- See Also:
- Constant Field Values
-
-
Method Detail
-
create
static java.util.Optional<AudioRecordingService> create()
Returns an instance ofAudioRecordingService
.- Returns:
- An instance of
AudioRecordingService
.
-
setAudioFolderName
void setAudioFolderName(java.lang.String folderName)
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
java.io.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.- Returns:
- a read only boolean property with the recording status
-
getAudioChunkFiles
javafx.beans.property.ReadOnlyListProperty<java.lang.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
-
-