use of com.codename1.components.AudioRecorderComponent in project CodenameOne by codenameone.
the class CodenameOneImplementation method captureAudio.
/**
* Captures a audio and notifies with the raw data when available
* @param response callback for the resulting data
*/
public void captureAudio(final MediaRecorderBuilder recordingOptions, final com.codename1.ui.events.ActionListener response) {
final MediaRecorderBuilder builder = recordingOptions == null ? new MediaRecorderBuilder() : recordingOptions;
if (!builder.isRedirectToAudioBuffer() && builder.getPath() == null) {
builder.path(new com.codename1.io.File("tmpaudio.wav").getAbsolutePath());
}
if (!builder.isRedirectToAudioBuffer() && builder.getMimeType() == null) {
builder.mimeType("audio/wav");
}
System.out.println("in captureAudio " + recordingOptions.isRedirectToAudioBuffer());
final AudioRecorderComponent cmp = new AudioRecorderComponent(builder);
final Sheet sheet = new Sheet(null, "Record Audio");
sheet.getContentPane().setLayout(new com.codename1.ui.layouts.BorderLayout());
sheet.getContentPane().add(com.codename1.ui.layouts.BorderLayout.CENTER, cmp);
cmp.addActionListener(new com.codename1.ui.events.ActionListener() {
@Override
public void actionPerformed(com.codename1.ui.events.ActionEvent e) {
switch(cmp.getState()) {
case Accepted:
CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {
public void run() {
sheet.back();
sheet.addCloseListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
sheet.removeCloseListener(this);
response.actionPerformed(new com.codename1.ui.events.ActionEvent(builder.getPath()));
}
});
}
});
break;
case Canceled:
FileSystemStorage fs = FileSystemStorage.getInstance();
if (fs.exists(builder.getPath())) {
FileSystemStorage.getInstance().delete(builder.getPath());
}
CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {
public void run() {
sheet.back();
sheet.addCloseListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
sheet.removeCloseListener(this);
response.actionPerformed(new com.codename1.ui.events.ActionEvent(null));
}
});
}
});
break;
}
}
});
sheet.addCloseListener(new com.codename1.ui.events.ActionListener() {
@Override
public void actionPerformed(com.codename1.ui.events.ActionEvent e) {
if (cmp.getState() != AudioRecorderComponent.RecorderState.Accepted && cmp.getState() != AudioRecorderComponent.RecorderState.Canceled) {
FileSystemStorage fs = FileSystemStorage.getInstance();
if (fs.exists(builder.getPath())) {
FileSystemStorage.getInstance().delete(builder.getPath());
}
CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {
public void run() {
response.actionPerformed(new com.codename1.ui.events.ActionEvent(null));
}
});
}
}
});
sheet.show();
// capture(response, new String[] {"wav", "mp3", "aac"}, "*.wav;*.mp3;*.aac");
}
use of com.codename1.components.AudioRecorderComponent in project CodenameOne by codenameone.
the class AudioRecorderComponentSample method recordAudio.
private AsyncResource<String> recordAudio() {
AsyncResource<String> out = new AsyncResource<>();
String mime = MediaManager.getAvailableRecordingMimeTypes()[0];
String ext = mime.indexOf("mp3") != -1 ? "mp3" : mime.indexOf("wav") != -1 ? "wav" : mime.indexOf("aiff") != -1 ? "aiff" : "aac";
MediaRecorderBuilder builder = new MediaRecorderBuilder().path(new File("myaudio." + ext).getAbsolutePath()).mimeType(mime);
final AudioRecorderComponent cmp = new AudioRecorderComponent(builder);
final Sheet sheet = new Sheet(null, "Record Audio");
sheet.getContentPane().setLayout(new com.codename1.ui.layouts.BorderLayout());
sheet.getContentPane().add(com.codename1.ui.layouts.BorderLayout.CENTER, cmp);
cmp.addActionListener(new com.codename1.ui.events.ActionListener() {
@Override
public void actionPerformed(com.codename1.ui.events.ActionEvent e) {
switch(cmp.getState()) {
case Accepted:
CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {
public void run() {
sheet.back();
sheet.addCloseListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
sheet.removeCloseListener(this);
out.complete(builder.getPath());
}
});
}
});
break;
case Canceled:
FileSystemStorage fs = FileSystemStorage.getInstance();
if (fs.exists(builder.getPath())) {
FileSystemStorage.getInstance().delete(builder.getPath());
}
CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {
public void run() {
sheet.back();
sheet.addCloseListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
sheet.removeCloseListener(this);
out.complete(null);
}
});
}
});
break;
}
}
});
sheet.addCloseListener(new com.codename1.ui.events.ActionListener() {
@Override
public void actionPerformed(com.codename1.ui.events.ActionEvent e) {
if (cmp.getState() != AudioRecorderComponent.RecorderState.Accepted && cmp.getState() != AudioRecorderComponent.RecorderState.Canceled) {
FileSystemStorage fs = FileSystemStorage.getInstance();
if (fs.exists(builder.getPath())) {
FileSystemStorage.getInstance().delete(builder.getPath());
}
CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {
public void run() {
out.complete(null);
}
});
}
}
});
sheet.show();
return out;
}
Aggregations