track.add, MidiEvent, and shortMessage (volume con

2019-08-29 02:31发布

On my client I am trying to make MIDI files fade out before continuing on to the next MIDI in the sequence, but I've been having trouble with it.

With the code below, it causes a nullpointerException at track.add, I was wondering what I am doing wrong?

I'm relatively new to Java programming, so please be specific in your answers

public void run() {
    active = true
    String s = findcachedir();
    uid = getuid(s);


            if(midiplay)
            {
                /*
                    midi = s + savereq;
                    midiplay = false;
                 */

                midi = s + savereq;
                try {
                    //System.out.println("Play MIDI " + midi);
                    if (musicSr != null)
                    {
                        musicSr.stop();
                        musicSr.close();
                    }
                    musicSr = null;
                    musicS = null;

                    File music = new File(midi);
                    if(music.exists())
                    {
                        musicS = MidiSystem.getSequence(music);
                    }
                    for (int k = 0; k < 16; k++) {
                        musicS.getTracks();
                        track.add(
                                new MidiEvent(
                                        new ShortMessage(
                                                ShortMessage.CONTROL_CHANGE,
                                                k, 
                                                7,
                                                20),
                                                track.getTicks()));
                    }

                        // Create a sequencer for the sequence
                        musicSr = MidiSystem.getSequencer();
                        musicSr.open();
                        musicSr.setSequence(musicS);
                        musicSr.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
                        musicSr.start();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }


                midiplay = false;

            }
            savereq = null;
        }

}


public static Sequencer musicSr = null;
Sequence musicS = null;

` The entire class if you need it for any reason

http://pastebin.com/6C7GNnib

1条回答
劳资没心,怎么记你
2楼-- · 2019-08-29 03:15

The variable track seems not to be initialized. May be you should do something like this :

private Track track = new Track();

or if you have getter setters implemented then set the value in track before using add method. Something like setTrack(trackObj);

查看更多
登录 后发表回答