|
13 | 13 | import com.codename1.payment.Purchase; |
14 | 14 | import com.codename1.l10n.L10NManager; |
15 | 15 | import com.codename1.location.LocationManager; |
| 16 | +import com.codename1.media.AudioBuffer; |
16 | 17 | import com.codename1.media.Media; |
| 18 | +import com.codename1.media.MediaManager; |
17 | 19 | import com.codename1.media.MediaRecorderBuilder; |
18 | 20 | import com.codename1.messaging.Message; |
19 | 21 | import com.codename1.notifications.LocalNotification; |
|
36 | 38 | import com.codename1.ui.geom.Rectangle; |
37 | 39 | import com.codename1.ui.geom.Shape; |
38 | 40 | import com.codename1.util.AsyncResource; |
| 41 | +import java.io.Closeable; |
39 | 42 |
|
40 | 43 | import java.io.ByteArrayInputStream; |
41 | 44 | import java.io.ByteArrayOutputStream; |
@@ -175,6 +178,7 @@ public class TestCodenameOneImplementation extends CodenameOneImplementation { |
175 | 178 | private String nextCaptureAudioPath = "file://test-audio.wav"; |
176 | 179 | private MediaRecorderBuilder lastMediaRecorderBuilder; |
177 | 180 | private VideoCaptureConstraints lastVideoConstraints; |
| 181 | + private final List<AudioCaptureFrame> audioCaptureFrames = new ArrayList<AudioCaptureFrame>(); |
178 | 182 |
|
179 | 183 |
|
180 | 184 | public TestCodenameOneImplementation() { |
@@ -1994,7 +1998,14 @@ public boolean exists(String file) { |
1994 | 1998 | public void rename(String file, String newName) { |
1995 | 1999 | TestFile f = fileSystem.remove(file); |
1996 | 2000 | if (f != null) { |
1997 | | - fileSystem.put(newName, f); |
| 2001 | + String target = newName; |
| 2002 | + if (newName != null && !newName.contains("://") && !newName.startsWith("/")) { |
| 2003 | + int lastSlash = file.lastIndexOf('/'); |
| 2004 | + if (lastSlash >= 0) { |
| 2005 | + target = file.substring(0, lastSlash + 1) + newName; |
| 2006 | + } |
| 2007 | + } |
| 2008 | + fileSystem.put(target, f); |
1998 | 2009 | } |
1999 | 2010 | } |
2000 | 2011 |
|
@@ -2166,6 +2177,17 @@ public void closingOutput(OutputStream stream) { |
2166 | 2177 | @Override |
2167 | 2178 | public void cleanup(Object obj) { |
2168 | 2179 | cleanupCalls.add(obj); |
| 2180 | + if (obj instanceof Closeable) { |
| 2181 | + try { |
| 2182 | + ((Closeable) obj).close(); |
| 2183 | + } catch (IOException ignored) { |
| 2184 | + } |
| 2185 | + } else if (obj instanceof AutoCloseable) { |
| 2186 | + try { |
| 2187 | + ((AutoCloseable) obj).close(); |
| 2188 | + } catch (Exception ignored) { |
| 2189 | + } |
| 2190 | + } |
2169 | 2191 | } |
2170 | 2192 |
|
2171 | 2193 | @Override |
@@ -3444,6 +3466,15 @@ public void captureAudio(MediaRecorderBuilder recordingOptions, ActionListener r |
3444 | 3466 | recordingOptions = new MediaRecorderBuilder(); |
3445 | 3467 | } |
3446 | 3468 | lastMediaRecorderBuilder = recordingOptions; |
| 3469 | + if (recordingOptions.isRedirectToAudioBuffer()) { |
| 3470 | + AudioBuffer buffer = MediaManager.getAudioBuffer(recordingOptions.getPath()); |
| 3471 | + if (buffer != null) { |
| 3472 | + for (AudioCaptureFrame frame : audioCaptureFrames) { |
| 3473 | + buffer.copyFrom(frame.getSampleRate(), frame.getNumChannels(), frame.getSamples()); |
| 3474 | + } |
| 3475 | + } |
| 3476 | + audioCaptureFrames.clear(); |
| 3477 | + } |
3447 | 3478 | response.actionPerformed(new ActionEvent(nextCaptureAudioPath)); |
3448 | 3479 | } |
3449 | 3480 |
|
@@ -3474,10 +3505,42 @@ public MediaRecorderBuilder getLastMediaRecorderBuilder() { |
3474 | 3505 | return lastMediaRecorderBuilder; |
3475 | 3506 | } |
3476 | 3507 |
|
| 3508 | + public void addAudioCaptureFrame(int sampleRate, int numChannels, float[] samples) { |
| 3509 | + audioCaptureFrames.add(new AudioCaptureFrame(sampleRate, numChannels, samples)); |
| 3510 | + } |
| 3511 | + |
| 3512 | + public void clearAudioCaptureFrames() { |
| 3513 | + audioCaptureFrames.clear(); |
| 3514 | + } |
| 3515 | + |
3477 | 3516 | public VideoCaptureConstraints getLastVideoConstraints() { |
3478 | 3517 | return lastVideoConstraints; |
3479 | 3518 | } |
3480 | 3519 |
|
| 3520 | + private static final class AudioCaptureFrame { |
| 3521 | + private final int sampleRate; |
| 3522 | + private final int numChannels; |
| 3523 | + private final float[] samples; |
| 3524 | + |
| 3525 | + private AudioCaptureFrame(int sampleRate, int numChannels, float[] samples) { |
| 3526 | + this.sampleRate = sampleRate; |
| 3527 | + this.numChannels = numChannels; |
| 3528 | + this.samples = Arrays.copyOf(samples, samples.length); |
| 3529 | + } |
| 3530 | + |
| 3531 | + private int getSampleRate() { |
| 3532 | + return sampleRate; |
| 3533 | + } |
| 3534 | + |
| 3535 | + private int getNumChannels() { |
| 3536 | + return numChannels; |
| 3537 | + } |
| 3538 | + |
| 3539 | + private float[] getSamples() { |
| 3540 | + return Arrays.copyOf(samples, samples.length); |
| 3541 | + } |
| 3542 | + } |
| 3543 | + |
3481 | 3544 | public static final class TestFile { |
3482 | 3545 | final boolean directory; |
3483 | 3546 | final byte[] content; |
|
0 commit comments