JMSL Tutorial: MusicJob

We present an alternative design practice to add custom functionality to a MusicJob. Instead of defining a subclass of MusicJob and overriding its repeat() method, we can use a stock MusicJob, and plug in a Playable at run time. Let's look at the Playable interface:
 public double play(double playTime, Composable thing)
Any class that satisfies the Playable interface can be added to a MusicJob's startPlayables, repeatPlayables, or stopPlayables. The "Composable thing" parameter in the play() method above will point to the parent who calls play. In this case it is our MusicJob.

You can create a Playable "on the fly" and add it to a MusicJob like this:

myMusicJob.addRepeatPlayable(new Playable() {
   public double play(double playTime, Composable thing) {
    // do something, anything
    return playTime;
   }
  });
 

Or you can define a Playable as is shown below:

/** A Playable that prints a message */

package jmsltutorial;
import com.softsynth.jmsl.*;

public class MessagePrintingPlayable implements Playable {
String message = "uninitialized";

 MessagePrintingPlayable(String msg) {
        message = msg;
 }

 public double play(double playTime, Composable thing) {
        JMSL.out.println(message);
        return playTime;
 }

}
Here's how you would add this to your MusicJob's repeatPlayables:
myMusicJob.addRepeatPlayable(new MessagePrintingPlayable());

Next, we will instantiate a few of these MessagePrintingPlayables and add
them into a MusicJob's start, repeat, and stop playables.

 
Previous Tutorial Index Tutorial Contents Next
  (C) 1997 Phil Burk and Nick Didkovsky, All Rights Reserved
  JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom.