SoundSample

Das Programm SoundSample spielt 8-bit- WAV Sounds über den internen Lautsprecher ab. Die Sounds werden über den NXT Manager hochgeladen und können über das Display ausgewählt werden

  1. import lejos.nxt.*;
  2. import lejos.util.TextMenu;
  3. import java.io.*;
  4. /**
  5. * Demonstrates playing 8-bit WAV files.
  6. *
  7. * Use nxjbrowse to upload 8-bit WAV files. On Windows XP, ringin.wav and
  8. * ringout.wav are 8-bit WAV file, which can be found in the Media subfolder of
  9. * the Windows folder.
  10. *
  11. * @author Lawrie Griffiths
  12. *
  13. */
  14. public class SoundSample {
  15. public static void main(String[] options) throws Exception {
  16. File[] allFiles = File.listFiles();
  17. String s;
  18. int len = 0;
  19. // Calculate number of WAV files
  20. for (int i = 0; i < allFiles.length && allFiles[i] != null; i++) {
  21. s = allFiles[i].getName();
  22. int l = s.length();
  23. if (l > 4 && s.charAt(l - 3) == 'w' && s.charAt(l - 2) == 'a'
  24. && s.charAt(l - 1) == 'v')
  25. len++;
  26. }
  27. // Make array of WAV files and fileNames
  28. String[] fileNames = new String[len];
  29. File[] files = new File[len];
  30. int j = 0;
  31. for (int i = 0; i < allFiles.length && allFiles[i] != null; i++) {
  32. s = allFiles[i].getName();
  33. int l = s.length();
  34. if (l > 4 && s.charAt(l - 3) == 'w' && s.charAt(l - 2) == 'a'
  35. && s.charAt(l - 1) == 'v') {
  36. fileNames[j] = s;
  37. files[j++] = allFiles[i];
  38. }
  39. }
  40. LCD.drawString("Play a WAV file", 0, 0);
  41. // Create menu of WAV files
  42. TextMenu fileMenu = new TextMenu(fileNames, 1);
  43. // Play files until the user quits
  44. int selected;
  45. do {
  46. selected = fileMenu.select();
  47. if (selected >= 0) {
  48. Sound.playSample(files[selected], 100);
  49. }
  50. } while (selected >= 0);
  51. }
  52. }