我是Objective C的新手,我正在尝试制作一个在按下按钮时播放midi文件的应用程序。我还希望它在文件播放时不响应额外的按钮按下。我制作了一个自定义按钮类并将此代码(来自教程)放入 .m 文件中:
- (IBAction)MIDItest:(id)sender
{
MusicSequence s;
NewMusicSequence(&s);
NSString *midiFilePath = [[NSBundle mainBundle]
pathForResource:@"eDom"
ofType:@"mid"];
NSURL *midiFileURL = [NSURL fileURLWithPath:midiFilePath];
MusicSequenceFileLoad(s, (__bridge CFURLRef)(midiFileURL), 0, 0);
MusicPlayer p;
NewMusicPlayer(&p);
MusicPlayerSetSequence(p, s);
MusicPlayerPreroll(p);
MusicPlayerStart(p);
MusicTrack t;
MusicTimeStamp len;
UInt32 sz = sizeof(MusicTimeStamp);
MusicSequenceGetIndTrack(s, 1, &t);
MusicTrackGetProperty(t, kSequenceTrackProperty_TrackLength, &len, &sz);
while (1) {
usleep (3 *1000 *1000);
MusicTimeStamp now = 0;
MusicPlayerGetTime(p, &now);
if (now >= len)
break;
}
MusicPlayerStop(p);
DisposeMusicSequence(s);
DisposeMusicPlayer(p);
}
但是,当我按下按钮时,什么也没有发生。我确实将按钮设置为自定义类,所以我认为问题出在 MIDI 播放器代码中。谁能告诉我我做错了什么以及如何解决这个问题?