[an error occurred while processing this directive]
Reflection
[an error occurred while processing this directive]
     Applet Source     LoadClasses.java     List methods of Player class      All lectures

Reflection Example
Play MP3 files from a remote Player library
Player is a class that supports playing MP3 files
It is part of library jl1.0.1.jar from javazoom that resides on gauss.ececs.uc.edu

Instuctions: Click "Choose" to open a JFileChooser. Select an MP3 file (sometimes the applet frame moves behind another window - I do not know why). Click "Play" to play the tune. The MP3 player is obtained remotely from gauss.ececs.uc.edu using java.lang.reflect classes.

import java.io.*;
import java.net.*;
import java.util.jar.*;
import java.util.*;
import java.lang.reflect.*;
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
class Loader extends Thread {
   String name, path;
   MP3Player mp3player;
   FileInputStream fis = null;
   Class  playit;
   Object player;
   Method play = null, close = null;

   public Loader (String path, String name, MP3Player mp3player) { 
      this.path = path;
      this.name = name;
      this.mp3player = mp3player;

      try { 
         // Open a connection to the file to play
         fis = new FileInputStream(path);
         BufferedInputStream bis = new BufferedInputStream(fis);

         // Get the player library, load the Player class, create an 
         // object of class Player, invoke method "play" on that object.
         String lib = 
            "http://gauss.ececs.uc.edu/Camp11/Reflect/RemoteLibrary/jl1.0.1.jar"; 
         URL url = new URL(lib); 
         URLClassLoader urlLoader = new URLClassLoader(new URL[] { url });
         playit = urlLoader.loadClass("javazoom.jl.player.Player");
         Constructor ctorlist[] = playit.getDeclaredConstructors();
         Constructor ct = ctorlist[0];
         player = ct.newInstance(bis);
         play = playit.getMethod("play");
         close = playit.getMethod("close");
      } catch (Exception e) { System.out.println(e); }
   }

   public void run () {
      try { play.invoke(player); } catch (Exception e) { }
      mp3player.redo();
      try { if (fis != null) fis.close(); } catch (Exception e) { }
   }
}

class Visuals extends JPanel implements Runnable {
   JLabel label;
   Thread runner;

   public Visuals (String name) {
      setLayout(new GridLayout(1,1));
      add(label = new JLabel(name, JLabel.CENTER));
      label.setForeground(new Color(245,190,110));
      label.setFont(new Font("TimesRoman",Font.PLAIN, 36));
   }

   public void suspend () { runner.suspend(); }

   public void resume () { runner.resume(); }

   public void start () {
      runner = new Thread(this);
      runner.start();
   }

   public void run () {
      float i=0;
      while (true) {
         if ((i+=0.5) == 100) i = 0;
         Color color = Color.getHSBColor((float)(i/100.0), 1, 1);
         label.setForeground(color);
         try { runner.sleep(10); } catch (Exception e) { }
      }
   }
}

class Controls extends JFrame implements ActionListener {
   JButton play, stop, choose, loop;
   Visuals visuals = null;
   MP3Player mp3 = null;

   public Controls (MP3Player mp3) {
      this.mp3 = mp3;
      setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.setLayout(new FlowLayout());
      p.add(choose = new JButton("Choose"));
      p.add(play = new JButton("   Play   "));
      p.add(stop = new JButton(" Stop "));
      p.add(loop = new JButton(" Loop "));
      add("North",p);

      add("Center", visuals = new Visuals(""));
      visuals.start();
      visuals.suspend();
      
      choose.addActionListener(this);
      play.addActionListener(this);
      stop.addActionListener(this);
      loop.addActionListener(this);
      loop.setBackground(Color.red);
   }

   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == choose) mp3.chooseIt();
      else if (evt.getSource() == play && mp3.suspended) mp3.playSelection();
      else if (evt.getSource() == play && !mp3.suspended) mp3.playSelectionS();
      else if (evt.getSource() == stop) mp3.stopIt();
      else if (evt.getSource() == loop && mp3.looping) mp3.loopIt();
      else if (evt.getSource() == loop && !mp3.looping) mp3.loopItS();
   }
}
 
public class MP3Player extends Applet {
   Controls cntl = null;
   String name = null, path = null, directory = "~";
   Loader loader = null;
   boolean looping = false, suspended = false;

   public void init () {
      cntl = new Controls(this);
      cntl.setSize(500,110);
      cntl.setVisible(true);
   }

   public void playIt () {
      loader = new Loader(path, name, this);
      loader.start();
      StringTokenizer t = new StringTokenizer(name,".\n");
      cntl.visuals.label.setText(t.nextToken());
      cntl.visuals.resume();
   }

   public void reset () {
      cntl.play.setText(" Play ");
      suspended = false;
      cntl.visuals.label.setText("");
      cntl.visuals.suspend();
   }

   public void redo () {  
      if (loader != null) { 
         try { loader.close.invoke(loader.player); } catch (Exception e) { }
         loader = null;
      }
      if (looping) playIt(); else reset();
   }

   public void chooseIt () {
      JFileChooser fd;
      try {
         fd = new JFileChooser(directory);
         int r;
         if ((r=fd.showOpenDialog(this)) == JFileChooser.APPROVE_OPTION) {
            File file = fd.getSelectedFile();
            name = file.getName();
            path = file.getAbsolutePath();
            directory = path.substring(0,path.lastIndexOf('/'));
         }
      } catch (Exception e) { }
   }

   public void playSelection () {
      suspended = false;
      cntl.play.setText("   Play   ");
      if (loader != null) loader.suspend();
      if (cntl.visuals != null) cntl.visuals.suspend();
   }

   public void playSelectionS () {
      suspended = true;
      cntl.play.setText("Pause");
      if (loader == null) playIt(); else loader.resume();
      if (cntl.visuals != null) cntl.visuals.resume();
   }

   public void stopIt () {
      if (loader != null) loader.stop();
      loader = null;
      reset();
   }

   public void loopIt () {
      looping = false;
      cntl.loop.setBackground(Color.red);
   }

   public void loopItS () {
      looping = true;
      cntl.loop.setBackground(Color.green);
   }
}