Transcript powerpoint
Large Systems
JavaSound
Basic Classes
public class CapturePlayback extends JPanel implements ActionListener, ControlContext {
public class Playback implements Runnable {
}
class Capture implements Runnable {
}
class FormatControls extends JPanel {
}
class SamplingGraph extends JPanel implements Runnable {
}
public static void main(String s[]) {
CapturePlayback capturePlayback = new CapturePlayback();
…
}
}
Basic Plan
• Add an AutoPlay button
• Create a thread for playing multiple files
• Wrap the run() method for playing a single file with
a loop that
– For all files in a directory
• Play the file
• Move the file
– Sleep/delay for a period of time before repeating the loop
• Use the shutdown() method to stop the loop
• Learn how to get a list of file from a directory
• Learn how to move a file from one directory to another
Add an AutoPlay button
1
…
JButton playB, captB, pausB, loadB;
…
playB = addButton("Play", buttonsPanel, false);
captB = addButton("Record", buttonsPanel, true);
2
…
if (playB.getText().startsWith("Play")) {
playback.start();
samplingGraph.start();
captB.setEnabled(false);
pausB.setEnabled(true);
playB.setText("Stop");
} else {
playback.stop();
samplingGraph.stop();
captB.setEnabled(true);
pausB.setEnabled(false);
playB.setText("Play");
}
3
public class Playback implements Runnable {
SourceDataLine line;
Thread thread;
public void start() {
errStr = null;
thread = new Thread(this);
thread.setName("Playback");
thread.start();
}
public void stop() {
thread = null;
}
Create a thread
private void shutDown(String message) {
if ((errStr = message) != null) {
System.err.println(errStr);
samplingGraph.repaint();
}
if (thread != null) {
thread = null;
samplingGraph.stop();
captB.setEnabled(true);
pausB.setEnabled(false);
playB.setText("Play");
}
}
Basic Plan
• Add an AutoPlay button
• Create a thread for playing multiple files
• Wrap the run() method for playing a single file with
a loop that
– For all files in a directory
• Play the file
• Move the file
– Sleep/delay for a period of time before repeating the loop
• Use the shutdown() method to stop the loop
• Learn how to get a list of file from a directory
• Learn how to move a file from one directory to another
run()
public void run() {
// reload the file if loaded by file
if (file != null) {
createAudioInputStream(file, false);
}
…
…
…
line = null;
shutDown(null);
}
Files and java
Search “java io” in google, first link is:
http://java.sun.com/j2se/1.3/docs/api/java/io/package-summary.html
This contains a link to the definition of the File class:
http://java.sun.com/j2se/1.3/docs/api/java/io/File.html
File class
File (String pathname);
String getName();
File[] listFiles();
boolean renameTo(File dest);
Finding an example
Not always simple
Search “java io” takes you to the general class defn
We should know that we want to see how to use renameTo()
From the previous overhead/File class .. But
Search “java renameTo example” and first link is
http://javaalmanac.com/egs/java.io/RenameFile.html?l=rel
(see next overhead)
renameTo
// File (or directory) with old name
File file = new File("oldname");
// File (or directory) with new name
File file2 = new File("newname");
// Rename file (or directory)
boolean success = file.renameTo(file2);
if (!success)
{ // File was not successfully renamed }
renameTo again
// File (or directory) to be moved
File file = new File("filename");
// Destination directory
File dir = new File("directoryname");
// Move file to new directory
boolean success =
file.renameTo(new File(dir, file.getName()));
if (!success)
{ // File was not successfully moved }
Basic Plan
• Add an AutoPlay button
• Create a thread for playing multiple files
• Wrap the run() method for playing a single file with
a loop that
– For all files in a directory
• Play the file
• Move the file
– Sleep/delay for a period of time before repeating the loop
• Use the shutdown() method to stop the loop
• Learn how to get a list of file from a directory
• Learn how to move a file from one directory to another