问题描述
在我的客户端,我试图让MIDI文件淡出,然后继续下一个MIDI序列,但我遇到麻烦。 >使用下面的代码,它会导致一个nullpointerException在 track.add
,我想知道我在做错什么?
我比较新的Java编程,所以请具体在你的答案
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;
文件音乐=新文件(midi);
if(music.exists())
{
musicS = MidiSystem.getSequence(music); (int k = 0; k musicS.getTracks();
}
track.add(
new MidiEvent(
new ShortMessage(
ShortMessage.CONTROL_CHANGE,
k,
7,
20),
track.getTicks()));
}
//为序列创建序列器
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;
序列musicS = null;
`
如果您因任何原因需要它,整个课程
变量 track
似乎没有被初始化。可能你应该这样做:
private Track track = new Track();
或者如果您实现了getter设置器,然后将值设置为 track
之前使用
添加
方法。像 setTrack(trackObj);
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
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);
这篇关于track.add,MidiEvent和shortMessage(音量控制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!