📄 javamedia.java
字号:
}); final JavaMedia demo = new JavaMedia(); f.getContentPane().add("Center", demo); f.pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int WIDTH = 310; int HEIGHT = 280; f.setLocation(screenSize.width/2 - WIDTH/2, screenSize.height/2 - HEIGHT/2); f.setSize(WIDTH, HEIGHT); f.show(); for (int i = 0; i < args.length; i++) { if (args[i].startsWith("-run")) { demo.start(); } } } public class JavaSound extends JPanel implements ActionListener { AudioClip clip = null; JButton startStopB, loopB; Timer timer = null; final int STOP = 0; final int PLAY = 1; final int LOOP = 2; int audioState = STOP; public JavaSound(String name, Color color) { clip = Applet.newAudioClip(getURL("media/", name)); EmptyBorder eb = new EmptyBorder(2,5,2,5); SoftBevelBorder sbb = new SoftBevelBorder(SoftBevelBorder.RAISED); setBorder(new CompoundBorder(eb, sbb)); setLayout(new BorderLayout()); JPanel p = new JPanel(new BorderLayout()); p.setBackground(Color.gray); p.setLayout(new BorderLayout()); JLabel l = new JLabel(name); l.setHorizontalAlignment(l.CENTER); l.setFont(new Font("serif",Font.BOLD, 14)); l.setForeground(color); p.add("North", l); JPanel p1 = new JPanel(new FlowLayout()); p1.setBackground(Color.gray); startStopB = new JButton(startIcon); startStopB.setPreferredSize(new Dimension(30, 20)); startStopB.setMargin(new Insets(0,0,0,0)); startStopB.addActionListener(this); p1.add(startStopB); loopB = new JButton(loopIcon); loopB.setMargin(new Insets(0,0,0,0)); loopB.setPreferredSize(new Dimension(30, 20)); loopB.addActionListener(this); p1.add(loopB); timer = new Timer(this, clip, color); p1.add(timer); p.add(p1); JToolBar toolbar = new JToolBar(); toolbar.setBackground(Color.gray); toolbar.add(p); add(toolbar); } public JButton addButton(ImageIcon ii) { JButton b = new JButton(ii); b.addActionListener(this); return b; } public void stopAudio() { audioState = STOP; clip.stop(); loopB.setIcon(loopIcon); startStopB.setIcon(startIcon); timer.stop(); } public void playAudio() { clip.play(); startStopB.setIcon(stopIcon); audioState = PLAY; timer.start(); } public void actionPerformed(ActionEvent evt) { if (evt.getSource().equals(loopB)) { clip.loop(); audioState = LOOP; timer.start(); loopB.setIcon(loopingIcon); startStopB.setIcon(stopIcon); } else if (audioState >= PLAY) { stopAudio(); } else { playAudio(); } } /** * Render the elapsed seconds of the clip during playback. */ public class Timer extends JComponent implements Runnable { private AudioClip clip; private boolean cbStop = true; private JavaMedia jm; private JavaSound js; private BufferedImage bimg; private Thread thread; private int theSeconds; private int w, h; private Font font = new Font("Dialog", Font.BOLD, 12); private Color color; public Timer(JavaSound js, AudioClip clip, Color color) { this.js = js; this.clip = clip; this.color = color; setBackground(Color.black); setPreferredSize(new Dimension(30, 20)); } public void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName("Mix.JavaMedia.JavaSound.Timer"); thread.start(); theSeconds = 0; } public synchronized void stop() { thread = null; notifyAll(); } public void run() { while (js.audioState != js.STOP) { repaint(); try { for (int i=0; i < 10 && js.audioState != js.STOP; i++) { Thread.sleep(95); // 95x10=950ms; almost a second } } catch(InterruptedException e){ break;} if (theSeconds++ > 99) { theSeconds = 0; } } if (js.audioState != js.STOP) { js.stopAudio(); } theSeconds = 0; repaint(); } public void paint(Graphics g) { if (bimg == null) { bimg = (BufferedImage) createImage(30, 20); } int w = bimg.getWidth(); int h = bimg.getHeight(); Graphics2D big = bimg.createGraphics(); big.setBackground(Color.black); big.clearRect(0,0,w,h); big.setFont(font); big.setColor(color); big.drawString(String.valueOf(theSeconds), 8,15); big.setColor(Color.gray); big.drawLine(0,0, 0,h-1); big.drawLine(0,0, w-1,0); big.setColor(Color.white); big.drawLine(w-1,0, w-1,h-1); big.drawLine(0,h-1, w-1,h-1); g.drawImage(bimg, 0, 0, this); big.dispose(); } } // End Timer class } // End JavaSound class// REMOVE TO USE JMF public class JMF extends JScrollPane { public JMF(String name) { String fs = System.getProperty("file.separator"); String s = "Java Media Framework\n" + "http://java.sun.com/products/java-media/jmf\n\n" + "Usage Instructions in \n" + "Java2D" + fs + "src" + fs + "demos" + fs + "Mix" + fs + "JavaMedia.java\n"; JTextArea textarea = new JTextArea(s); textarea.setFont(new Font("serif", Font.PLAIN, 10)); textarea.setEditable(false); getViewport().add(textarea); } }// REMOVE TO HERE/* UNCOMMENT TO USE JMF public class JMF extends Panel implements ControllerListener { Player player; boolean rewoundToggle; public JMF(String name) { setLayout(new BorderLayout()); URL url = getURL("media/", name); if (url != null) { try { player = Manager.createPlayer(url); } catch (Exception e) { System.err.println("Can't create Manager because: " + e); } player.addControllerListener(this); } else { String fs = System.getProperty("file.separator"); String s = "Couldn't locate : \n" + "Java2D" + fs + "demos" + fs + "Mix" + fs + name; JTextArea textarea = new JTextArea(s); textarea.setFont(new Font("serif", Font.PLAIN, 10)); textarea.setEditable(false); JScrollPane scroller = new JScrollPane(); scroller.getViewport().add(textarea); add(scroller); } } public void start() { invalidate(); if (player != null) { player.start(); } validate(); } public void stop() { if (player != null && player.getState() == player.Started) { player.stop(); player.deallocate(); } } public synchronized void controllerUpdate(ControllerEvent event) { // If we're getting messages from a dead player, just leave if (player == null) return; // When the player is Realized, get the visual // and control components and add them to the Panel if (event instanceof RealizeCompleteEvent) { add(player.getVisualComponent()); add("South", player.getControlPanelComponent()); validate(); } else if (event instanceof EndOfMediaEvent) { // We've reached the end of the media; rewind & start over rewoundToggle = true; player.setMediaTime(new Time(0)); player.start(); } else if (event instanceof ControllerErrorEvent) { player = null; } } } // End JMF classEND UNCOMMENT */} // End JavaMedia class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -