When playing music, AudioClip object is needed, which play *.au and *.wav files.
There are three methods on AudioClip objects:
• play(): it plays the audio file once and stop.
• loop(): it plays the audio file repeatedly until the stop method is called.
• stop(): it stops a loop play of an audio clip.
To test the code, get any wav files, rename them to shutter.wav and tuner2.wav, then copy them to your DocumentBase directory (just uncomment //System.out.println("DocumentBase="+getDocumentBase()); to find out).
Show Code   Hide Code
/*music.java*/ package AppletExample; import java.applet.AudioClip; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JApplet; public class music extends JApplet { AudioClip buttonSound, musicLoop; public void init() { setLayout(new FlowLayout(FlowLayout.CENTER)); Button playBtn; add (playBtn = new Button("Play")); playBtn.addActionListener( new BtnAdapter (0)); Button loopBtn; add (loopBtn = new Button("Loop")); loopBtn.addActionListener( new BtnAdapter(1)); Button stopBtn; add (stopBtn = new Button("Stop")); stopBtn.addActionListener( new BtnAdapter (2)); // System.out.println("DocumentBase="+getDocumentBase()); buttonSound = getAudioClip(getDocumentBase(), "shutter.wav"); musicLoop = getAudioClip(getDocumentBase(), "tuner2.wav"); } public void start() { // music restart when back to the page musicLoop.loop(); } public void stop() { // music stops when move away musicLoop.stop(); } class BtnAdapter implements ActionListener { private int id; BtnAdapter( int buttonID) { id = buttonID; } public void actionPerformed (ActionEvent e) { switch (id) { case 0: buttonSound.play(); break; case 1: musicLoop.loop(); break; case 2: musicLoop.stop(); } } } } |
Play Video in JApplet
When playing video a Manager class and a player object are needed.
There are there control methods on the player object:
player.start()
player.stop()
player.close()
Java Media Framework (JMF) API is needed for import javax.media.*;
You need to download JMF library for compiling the code.
To test the code, get any mpg file, rename it to phantom.mpg, then copy it to your DocumentBase directory (just uncomment //System.out.println("DocumentBase="+getDocumentBase()); to find out).
Show Code   Hide Code
/*videoPlayer.java*/ package AppletExample; import javax.media.*; import java.awt.*; import java.net.URL; import javax.swing.JApplet; public class videoPlayer extends JApplet { Player player; public void init() { URL mediaURL = null; try { // System.out.println("DocumentBase="+getDocumentBase()); mediaURL = new URL(getDocumentBase(),"phantom.mpg"); player = Manager.createRealizedPlayer(mediaURL); } catch (Exception e) { e.printStackTrace(); } Component video = player.getVisualComponent(); Component controls = player.getControlPanelComponent(); add("Center", video); add("South", controls); } public void start() { if (player != null) player.start(); } public void stop() { if (player != null) player.stop(); } } |
download the supporting files and code from here.
No comments:
Post a Comment