- May 5, 2005
- 169
- 0
- 0
Please take a look at the following code (pulled from Head First Java 2nd ed).
This is how the code is written in the book, but it does not compile:
import javax.sound.midi.*;
public class MiniMiniMusicApp
{
public static void main (String [] args)
{
MiniMiniMusicApp mini = new MiniMiniMusicApp();
mini.play();
}
public void play()
{
try
{
Sequencer player = MidiSystem.getSequencer();
player.open();
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
ShortMessage a = new ShortMessage();
ShortMessage first = new ShortMessage();
first.setMessage(192,1,122,0);
MidiEvent changeInstrument = new MidiEvent(first, 1);
track.add(changeInstrument);
a.setMessage(144, 1, 20, 125);
MidiEvent noteOn = new MidiEvent(a, 1);
track.add(noteOn);
ShortMessage b = new ShortMessage();
b.setMessage(128, 1, 20, 125);
MidiEvent noteOff = new MidiEvent(b, 16);
track.add(noteOff);
player.setSequence(seq);
player.start();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
The code above returns the following errors when attempting to compile:
MiniMiniMusicApp.java:20: cannot find symbol
symbol : variable PPQ
location: class Sequence
Sequence seq = new Sequence(Sequence.PPQ, 4);
^
MiniMiniMusicApp.java:20: internal error; cannot instantiate Sequence.<init> at
Sequence to ()
Sequence seq = new Sequence(Sequence.PPQ, 4);
^
MiniMiniMusicApp.java:22: cannot find symbol
symbol : method createTrack()
location: class Sequence
Track track = seq.createTrack();
^
MiniMiniMusicApp.java:39: cannot find symbol
symbol : method setSequence(Sequence)
location: interface javax.sound.midi.Sequencer
player.setSequence(seq);
If I go back and simply add "import javax.sound.midi.Sequence;" right under the import.javax.midi.*; statement, then the problem compiles and runs.
My question is why do I have to explicitly have the line import.javax.sound.midi.Sequence IN ADDITION to import.javax.midi.* in order for the program to work.
This appears to be redundant
import javax.sound.midi.*;
import javax.sound.midi.Sequence;
Since the * wilcard should include everything in that package automatically. However this is the only way the program works.
Please explain someone.
This is how the code is written in the book, but it does not compile:
import javax.sound.midi.*;
public class MiniMiniMusicApp
{
public static void main (String [] args)
{
MiniMiniMusicApp mini = new MiniMiniMusicApp();
mini.play();
}
public void play()
{
try
{
Sequencer player = MidiSystem.getSequencer();
player.open();
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
ShortMessage a = new ShortMessage();
ShortMessage first = new ShortMessage();
first.setMessage(192,1,122,0);
MidiEvent changeInstrument = new MidiEvent(first, 1);
track.add(changeInstrument);
a.setMessage(144, 1, 20, 125);
MidiEvent noteOn = new MidiEvent(a, 1);
track.add(noteOn);
ShortMessage b = new ShortMessage();
b.setMessage(128, 1, 20, 125);
MidiEvent noteOff = new MidiEvent(b, 16);
track.add(noteOff);
player.setSequence(seq);
player.start();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
The code above returns the following errors when attempting to compile:
MiniMiniMusicApp.java:20: cannot find symbol
symbol : variable PPQ
location: class Sequence
Sequence seq = new Sequence(Sequence.PPQ, 4);
^
MiniMiniMusicApp.java:20: internal error; cannot instantiate Sequence.<init> at
Sequence to ()
Sequence seq = new Sequence(Sequence.PPQ, 4);
^
MiniMiniMusicApp.java:22: cannot find symbol
symbol : method createTrack()
location: class Sequence
Track track = seq.createTrack();
^
MiniMiniMusicApp.java:39: cannot find symbol
symbol : method setSequence(Sequence)
location: interface javax.sound.midi.Sequencer
player.setSequence(seq);
If I go back and simply add "import javax.sound.midi.Sequence;" right under the import.javax.midi.*; statement, then the problem compiles and runs.
My question is why do I have to explicitly have the line import.javax.sound.midi.Sequence IN ADDITION to import.javax.midi.* in order for the program to work.
This appears to be redundant
import javax.sound.midi.*;
import javax.sound.midi.Sequence;
Since the * wilcard should include everything in that package automatically. However this is the only way the program works.
Please explain someone.