use of com.codename1.media.WAVWriter in project CodenameOne by codenameone.
the class AudioBufferSample method start.
public void start() {
if (current != null) {
current.show();
return;
}
Form hi = new Form("Capture", BoxLayout.y());
hi.setToolbar(new Toolbar());
Style s = UIManager.getInstance().getComponentStyle("Title");
FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_MIC, s);
FileSystemStorage fs = FileSystemStorage.getInstance();
String recordingsDir = fs.getAppHomePath() + "recordings/";
fs.mkdir(recordingsDir);
try {
for (String file : fs.listFiles(recordingsDir)) {
MultiButton mb = new MultiButton(file.substring(file.lastIndexOf("/") + 1));
mb.addActionListener((e) -> {
try {
Media m = MediaManager.createMedia(recordingsDir + file, false);
m.play();
} catch (Throwable err) {
Log.e(err);
}
});
hi.add(mb);
}
hi.getToolbar().addCommandToRightBar("", icon, (ev) -> {
try {
String path = "tmpBuffer.pcm";
int wavSampleRate = 16000;
WAVWriter wavFileWriter = new WAVWriter(new File("tmpBuffer.wav"), wavSampleRate, 1, 16);
AudioBuffer audioBuffer = MediaManager.getAudioBuffer(path, true, 64);
MediaRecorderBuilder options = new MediaRecorderBuilder().audioChannels(1).redirectToAudioBuffer(true).path(path);
System.out.println("Builder isredirect? " + options.isRedirectToAudioBuffer());
float[] byteBuffer = new float[audioBuffer.getMaxSize()];
audioBuffer.addCallback(buf -> {
if (buf.getSampleRate() > wavSampleRate) {
buf.downSample(wavSampleRate);
}
buf.copyTo(byteBuffer);
try {
wavFileWriter.write(byteBuffer, 0, buf.getSize());
} catch (Throwable t) {
Log.e(t);
}
});
String file = Capture.captureAudio(options);
wavFileWriter.close();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MMM-dd-kk-mm");
String fileName = sd.format(new Date());
String filePath = recordingsDir + fileName;
Util.copy(fs.openInputStream(new File("tmpBuffer.wav").getAbsolutePath()), fs.openOutputStream(filePath));
MultiButton mb = new MultiButton(fileName);
mb.addActionListener((e) -> {
try {
Media m = MediaManager.createMedia(filePath, false);
m.play();
} catch (IOException err) {
Log.e(err);
}
});
hi.add(mb);
hi.revalidate();
if (file != null) {
System.out.println(file);
}
} catch (Throwable err) {
Log.e(err);
}
});
} catch (Throwable err) {
Log.e(err);
}
hi.show();
}
Aggregations