Created
March 5, 2016 19:01
-
-
Save codenameone/a347dc9dcadaa759d0cb to your computer and use it in GitHub Desktop.
Demonstrates capturing of audio files and their playback using the Codename One API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(IOException err) { | |
Log.e(err); | |
} | |
}); | |
hi.add(mb); | |
} | |
hi.getToolbar().addCommandToRightBar("", icon, (ev) -> { | |
try { | |
String file = Capture.captureAudio(); | |
if(file != null) { | |
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MMM-dd-kk-mm"); | |
String fileName =sd.format(new Date()); | |
String filePath = recordingsDir + fileName; | |
Util.copy(fs.openInputStream(file), 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(); | |
} | |
} catch(IOException err) { | |
Log.e(err); | |
} | |
}); | |
} catch(IOException err) { | |
Log.e(err); | |
} | |
hi.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm having an exception when I try to record for the 2nd time, can you help?
Exception: javax.sound.sampled.LineUnavailableException - line with format PCM_SIGNED 44100.0 Hz, 8 bit, mono, 1 bytes/frame, not supported.