从 MusicSequence 生成 MIDI 文件时遇到问题

Having trouble generating MIDI file from MusicSequence

我正在尝试从一个简单的 MusicSequence 生成一个 MIDI 文件。这是我的代码:

MusicSequence mySequence;
MusicTrack myTrack;
NewMusicSequence(&mySequence);
MusicSequenceNewTrack(mySequence, &myTrack);

MIDINoteMessage noteMessage;
MusicTimeStamp timestamp = 0;
noteMessage.channel = 0;
noteMessage.note = 60;
noteMessage.velocity = 90;
noteMessage.releaseVelocity = 0;
noteMessage.duration = 4;

if (MusicTrackNewMIDINoteEvent(myTrack, timestamp, &noteMessage) != noErr) NSLog(@"ERROR creating the note");
else NSLog(@"Note added");

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                    NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *midiPath = [documentsDirectory
                    stringByAppendingPathComponent:@"testfile.mid"];
CFURLRef midiURL = (__bridge CFURLRef)([[NSURL alloc] initFileURLWithPath:midiPath]);

OSStatus status = 0;

status = MusicSequenceFileCreate(mySequence, midiURL, kMusicSequenceFile_MIDIType, kMusicSequenceFileFlags_EraseFile, 0);

if(status != noErr){
    printf("Error on create: %d\n", (int)status);
    status = 0;
}

我的代码在 MusicSequenceFileCreate 失败。

这是我得到的错误:

Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

我怎样才能克服这个问题?

这似乎是 MIDI 定义的错误 URL。我将该行更改为此,现在可以使用了。

CFURLRef midiURL = (__bridge CFURLRef)([NSURL fileURLWithPath:midiPath]);