Play audio with a Ruby shell script
1 2 #!/usr/bin/ruby 3 4 #file: ogg-play.rb 5 6 `ogg123 #{ARGV[0]} > /dev/null 2>&1` 7
e.g.
1 2 wget http://upload.wikimedia.org/wikipedia/en/4/4d/Elo_blue_sky.ogg 3 ./ogg-play.rb Elo_blue_sky.ogg
12977 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
1 2 #!/usr/bin/ruby 3 4 #file: ogg-play.rb 5 6 `ogg123 #{ARGV[0]} > /dev/null 2>&1` 7
1 2 wget http://upload.wikimedia.org/wikipedia/en/4/4d/Elo_blue_sky.ogg 3 ./ogg-play.rb Elo_blue_sky.ogg
1 2 package Multimedia; 3 4 import javax.swing.JDialog; 5 import javax.swing.JFrame; 6 import javax.swing.UIManager; 7 import javax.swing.UnsupportedLookAndFeelException; 8 import javax.swing.plaf.metal.MetalLookAndFeel; 9 10 public class AudioSystem 11 { 12 public static void main(String[] args) 13 { 14 JFrame.setDefaultLookAndFeelDecorated(true); 15 JDialog.setDefaultLookAndFeelDecorated(true); 16 17 try 18 { 19 UIManager.setLookAndFeel(new MetalLookAndFeel()); 20 } 21 catch(UnsupportedLookAndFeelException e) 22 { 23 e.printStackTrace(); 24 } 25 26 new AudioGUI(); 27 } 28 }
1 2 package Multimedia; 3 4 import java.awt.Container; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.awt.event.WindowEvent; 8 import java.awt.event.WindowListener; 9 import java.io.File; 10 11 import javax.swing.JButton; 12 import javax.swing.JFileChooser; 13 import javax.swing.JFrame; 14 import javax.swing.JPanel; 15 16 public class AudioGUI extends JFrame implements WindowListener 17 { 18 public AudioGUI() 19 { 20 this.setTitle("AudioSystem"); 21 this.setSize(240, 100); 22 this.setResizable(false); 23 24 this.addWindowListener(this); 25 26 Container gc = this.getContentPane(); 27 gc.add(new AudioGUIPan()); 28 29 this.setVisible(true); 30 } 31 32 public void windowOpened(WindowEvent e) 33 { 34 35 } 36 37 public void windowClosing(WindowEvent e) 38 { 39 System.exit(0); 40 } 41 42 public void windowClosed(WindowEvent e) 43 { 44 45 } 46 47 public void windowIconified(WindowEvent e) 48 { 49 50 } 51 52 public void windowDeiconified(WindowEvent e) 53 { 54 55 } 56 57 public void windowActivated(WindowEvent e) 58 { 59 60 } 61 62 public void windowDeactivated(WindowEvent e) 63 { 64 65 } 66 } 67 68 class AudioGUIPan extends JPanel implements ActionListener 69 { 70 private JButton play; 71 private JButton stop; 72 private JButton loop; 73 private JButton open; 74 private JButton close; 75 76 private JFileChooser jfc; 77 78 private String file; 79 80 private AudioDevice ad; 81 82 public AudioGUIPan() 83 { 84 play = new JButton("Play"); 85 play.addActionListener(this); 86 stop = new JButton("Stop"); 87 stop.addActionListener(this); 88 open = new JButton("Open"); 89 open.addActionListener(this); 90 close = new JButton("Close"); 91 close.addActionListener(this); 92 93 this.add(stop); 94 this.add(play); 95 this.add(open); 96 this.add(close); 97 } 98 99 public void actionPerformed(ActionEvent e) 100 { 101 if(e.getSource() == close) 102 { 103 System.exit(0); 104 } 105 else if(e.getSource() == open) 106 { 107 jfc = new JFileChooser(); 108 jfc.setFileFilter(new AudioFilter()); 109 110 if(jfc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) 111 { 112 file = jfc.getSelectedFile().getAbsolutePath(); 113 ad = new AudioDevice(new File(file)); 114 } 115 } 116 else if(e.getSource() == play) 117 { 118 if(ad != null) 119 ad.play(); 120 } 121 else if(e.getSource() == stop) 122 { 123 if(ad != null) 124 ad.stop(); 125 } 126 } 127 }
1 2 package Multimedia; 3 import java.io.File; 4 5 import javax.swing.filechooser.FileFilter; 6 7 public class AudioFilter extends FileFilter 8 { 9 public boolean accept(File f) 10 { 11 return f.getName().toLowerCase().endsWith(".wav") || f.isDirectory(); 12 } 13 14 public String getDescription() 15 { 16 return "Audio File *.wav"; 17 } 18 }
1 2 package Multimedia; 3 4 import java.applet.Applet; 5 import java.applet.AudioClip; 6 import java.io.File; 7 import java.net.MalformedURLException; 8 9 public class AudioDevice implements AudioClip 10 { 11 private AudioClip ac; 12 public AudioDevice(File f) 13 { 14 try 15 { 16 ac = Applet.newAudioClip(f.toURL()); 17 } 18 catch(MalformedURLException e) 19 { 20 e.printStackTrace(); 21 } 22 } 23 24 public void play() 25 { 26 ac.play(); 27 } 28 29 public void stop() 30 { 31 ac.stop(); 32 } 33 34 public void loop() 35 { 36 37 } 38 }