Search in sources :

Example 1 with AudioRecorderComponent

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");
}
Also used : MediaRecorderBuilder(com.codename1.media.MediaRecorderBuilder) ActionEvent(com.codename1.ui.events.ActionEvent) FileSystemStorage(com.codename1.io.FileSystemStorage) ActionEvent(com.codename1.ui.events.ActionEvent) ActionListener(com.codename1.ui.events.ActionListener) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) com.codename1.ui(com.codename1.ui) AudioRecorderComponent(com.codename1.components.AudioRecorderComponent)

Example 2 with AudioRecorderComponent

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;
}
Also used : MediaRecorderBuilder(com.codename1.media.MediaRecorderBuilder) ActionEvent(com.codename1.ui.events.ActionEvent) FileSystemStorage(com.codename1.io.FileSystemStorage) ActionEvent(com.codename1.ui.events.ActionEvent) ActionListener(com.codename1.ui.events.ActionListener) ActionListener(com.codename1.ui.events.ActionListener) AudioRecorderComponent(com.codename1.components.AudioRecorderComponent) AsyncResource(com.codename1.util.AsyncResource) File(com.codename1.io.File) Sheet(com.codename1.ui.Sheet)

Aggregations

AudioRecorderComponent (com.codename1.components.AudioRecorderComponent)2 FileSystemStorage (com.codename1.io.FileSystemStorage)2 MediaRecorderBuilder (com.codename1.media.MediaRecorderBuilder)2 ActionEvent (com.codename1.ui.events.ActionEvent)2 ActionListener (com.codename1.ui.events.ActionListener)2 File (com.codename1.io.File)1 com.codename1.ui (com.codename1.ui)1 Sheet (com.codename1.ui.Sheet)1 BorderLayout (com.codename1.ui.layouts.BorderLayout)1 AsyncResource (com.codename1.util.AsyncResource)1