Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
shai-almog committed Oct 22, 2019
2 parents 57ef476 + da64ac1 commit 806f2f7
Show file tree
Hide file tree
Showing 18 changed files with 1,569 additions and 143 deletions.
31 changes: 31 additions & 0 deletions CodenameOne/src/com/codename1/capture/Capture.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.codename1.io.FileSystemStorage;
import com.codename1.io.Log;
import com.codename1.io.Util;
import com.codename1.media.MediaRecorderBuilder;
import com.codename1.ui.Display;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
Expand Down Expand Up @@ -95,6 +96,18 @@ public static String captureAudio() {
Display.getInstance().invokeAndBlock(c);
return c.url;
}

/**
* Same as {@link #captureAudio(com.codename1.media.MediaRecorderBuilder) { only a blocking version that holds the EDT
* @return the audio file location or null if the user canceled
* @since 7.0
*/
public static String captureAudio(MediaRecorderBuilder recordingOptions) {
CallBack c = new CallBack();
captureAudio(recordingOptions, c);
Display.getInstance().invokeAndBlock(c);
return c.url;
}


/**
Expand Down Expand Up @@ -178,6 +191,24 @@ public static String capturePhoto(int width, int height) {
public static void captureAudio(ActionListener response){
Display.getInstance().captureAudio(response);
}

/**
* This method tries to invoke the device native hardware to capture audio.
* The method returns immediately and the response will be sent asynchronously
* to the given ActionListener Object
* The audio record settings are specified in the recorderOptions parameter.
*
* <p>use this in the actionPerformed to retrieve the file path.
* String path = (String) evt.getSource();</p>
*
*
* @param response a callback Object to retrieve the file path
* @throws RuntimeException if this feature failed or unsupported on the platform
* @since 7.0
*/
public static void captureAudio(MediaRecorderBuilder recorderOptions, ActionListener response){
Display.getInstance().captureAudio(recorderOptions, response);
}

/**
* Captures video with some constraints, like width, height, and max length. Video constraints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ public void actionPerformed(ActionEvent t) {
done.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent t) {

if (builder.isRedirectToAudioBuffer()) {
// We were just redirecting to the audio buffer so we don't have any previews to speak of
media.cleanup();
setState(RecorderState.Paused);
setState(RecorderState.Accepted);
return;
}

final boolean[] closeHandled = new boolean[1];
media.pause();
setState(RecorderState.Paused);
Expand Down
166 changes: 96 additions & 70 deletions CodenameOne/src/com/codename1/components/Switch.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public class Switch extends Component {
private Image trackOffImage;
private Image trackDisabledImage;
private boolean dragged;
private int pressX;
private int deltaX; //pressX - currentdragX
private int pressX, pressY;
private int deltaX, deltaY; //pressX - currentdragX
private final EventDispatcher dispatcher = new EventDispatcher();
private final EventDispatcher changeDispatcher = new EventDispatcher();
private boolean animationLock;
Expand Down Expand Up @@ -627,89 +627,108 @@ private static int getAlignedCoord(int coord, int parentDim, int elemDim, int al
*/
protected void initComponent() {
super.initComponent();
addPointerPressedListener(pointerPressed);
addPointerDraggedListener(pointerDragged);
addPointerReleasedListener(pointerReleased);
}

/**
* {@inheritDoc}
*/
protected void deinitialize() {
removePointerPressedListener(pointerPressed);
removePointerDraggedListener(pointerDragged);
removePointerReleasedListener(pointerReleased);
super.deinitialize();
}

/**
* {@inheritDoc}
*/
public void pointerPressed(int x, int y) {
super.pointerPressed(x, y);
pressX = x;
}

/**
* {@inheritDoc}
*/
public void pointerDragged(int x, int y) {
dragged = true;
deltaX = pressX - x;
}

/**
* {@inheritDoc}
*/
public void pointerReleased(int x, int y) {
if (animationLock) {
return;
private final ActionListener pointerPressed = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
dragged = false;
deltaX = 0;
deltaY = 0;
pressX = evt.getX();
pressY = evt.getY();
}
animationLock = true;
};

private final ActionListener pointerDragged = new ActionListener() {
public void actionPerformed(ActionEvent evt) {

if (dragged) {
if (deltaX > 0) { //dragged from RtL
int trackLength = 0;
if (isRTL()) {
trackLength = getCurrentTrackOffImage().getWidth() - getCurrentThumbImage().getWidth();
} else {
trackLength = getCurrentTrackOnImage().getWidth() - getCurrentThumbImage().getWidth();
}
if (deltaX > trackLength / 2) {
animateTo(isRTL(), deltaX, trackLength, trackLength);
} else { //Not moved enaugh, go back to the state where we come from (i.e. ON for LtR switch and OFF for RtL ones)
animateTo(!isRTL(), deltaX, 0, trackLength);
}
} else { //dragged from LtR
int trackLength = 0;
if (isRTL()) {
trackLength = getCurrentTrackOnImage().getWidth() - getCurrentThumbImage().getWidth();
} else {
trackLength = getCurrentTrackOffImage().getWidth() - getCurrentThumbImage().getWidth();
}
if (deltaX * -1 > trackLength / 2) {
animateTo(!isRTL(), deltaX, -trackLength, trackLength);
} else {
animateTo(isRTL(), deltaX, 0, trackLength);
}
dragged = true;
deltaX = pressX - evt.getX();
deltaY = pressY - evt.getY();
if (Math.abs(deltaY) >= Math.abs(deltaX*0.5)) {
dragged = false;
deltaX = 0;
deltaY = 0;
} else {
evt.consume();
}
} else {
if (value) {
int trackLength = 0;
if (isRTL()) {
trackLength = getCurrentTrackOnImage().getWidth() - getCurrentThumbImage().getWidth();
} else {
trackLength = getCurrentTrackOffImage().getWidth() - getCurrentThumbImage().getWidth();
}
animateTo(false, 0, trackLength, trackLength);
}
};

private final ActionListener pointerReleased = new ActionListener() {

public void actionPerformed(ActionEvent evt) {
if (animationLock) {
return;
}
animationLock = true;

if (dragged) {
if (deltaX > 0) { //dragged from RtL
int trackLength = 0;
if (isRTL()) {
trackLength = getCurrentTrackOffImage().getWidth() - getCurrentThumbImage().getWidth();
} else {
trackLength = getCurrentTrackOnImage().getWidth() - getCurrentThumbImage().getWidth();
}
if (deltaX > trackLength / 2) {
animateTo(isRTL(), deltaX, trackLength, trackLength);
} else { //Not moved enaugh, go back to the state where we come from (i.e. ON for LtR switch and OFF for RtL ones)
animateTo(!isRTL(), deltaX, 0, trackLength);
}
} else { //dragged from LtR
int trackLength = 0;
if (isRTL()) {
trackLength = getCurrentTrackOnImage().getWidth() - getCurrentThumbImage().getWidth();
} else {
trackLength = getCurrentTrackOffImage().getWidth() - getCurrentThumbImage().getWidth();
}
if (deltaX * -1 > trackLength / 2) {
animateTo(!isRTL(), deltaX, -trackLength, trackLength);
} else {
animateTo(isRTL(), deltaX, 0, trackLength);
}
}
} else {
int trackLength = 0;
if (isRTL()) {
trackLength = getCurrentTrackOffImage().getWidth() - getCurrentThumbImage().getWidth();
if (value) {
int trackLength = 0;
if (isRTL()) {
trackLength = getCurrentTrackOnImage().getWidth() - getCurrentThumbImage().getWidth();
} else {
trackLength = getCurrentTrackOffImage().getWidth() - getCurrentThumbImage().getWidth();
}
animateTo(false, 0, trackLength, trackLength);
} else {
trackLength = getCurrentTrackOnImage().getWidth() - getCurrentThumbImage().getWidth();
int trackLength = 0;
if (isRTL()) {
trackLength = getCurrentTrackOffImage().getWidth() - getCurrentThumbImage().getWidth();
} else {
trackLength = getCurrentTrackOnImage().getWidth() - getCurrentThumbImage().getWidth();
}
animateTo(true, 0, -trackLength, trackLength);
}
animateTo(true, 0, -trackLength, trackLength);
}

}
animationLock = false;
return;
}
animationLock = false;
return;
}
};




private void animateTo(final boolean value, final int deltaStart, final int deltaEnd, final int maxMoveDist) {
int anim_duration = (int) Math.abs((deltaEnd - deltaStart) / (double) maxMoveDist * 100.0);
Expand All @@ -723,6 +742,8 @@ public boolean animate() {
dragged = true;
if (current.isFinished()) {
dragged = false;
deltaX = 0;
deltaY = 0;
Form f = getComponentForm();
if (f != null) {
f.deregisterAnimated(this);
Expand All @@ -736,11 +757,15 @@ public boolean animate() {
public void paint(Graphics g) {
}
});
dragged = true;
} else {
deltaX = deltaEnd;
//deltaX = deltaEnd;
deltaX = 0;
deltaY = 0;
dragged = false;
setValue(value, true);
}
dragged = true;

}

/**
Expand Down Expand Up @@ -839,6 +864,7 @@ public Object getComponentState() {
* @param state the non-null state
*/
public void setComponentState(Object state) {
System.out.println("Setting component state "+state);
value = ((Boolean) state).booleanValue();
}

Expand Down
23 changes: 16 additions & 7 deletions CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java
Original file line number Diff line number Diff line change
Expand Up @@ -5360,19 +5360,28 @@ public Image captureScreen() {
}
return null;
}


public void captureAudio(final com.codename1.ui.events.ActionListener response) {
captureAudio( new MediaRecorderBuilder()
.path(new com.codename1.io.File("tmpaudio.wav").getAbsolutePath())
.mimeType("audio/wav"), response);

}


/**
* Captures a audio and notifies with the raw data when available
* @param response callback for the resulting data
*/
public void captureAudio(final com.codename1.ui.events.ActionListener response) {

final MediaRecorderBuilder builder = new MediaRecorderBuilder()
.path(new com.codename1.io.File("tmpaudio.wav").getAbsolutePath())
.mimeType("audio/wav");

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());
Expand Down
Loading

0 comments on commit 806f2f7

Please sign in to comment.