2

我目前正在开发一个需要根据用户输入创建 MusicSequence 的应用程序,并且我目前在 MusicSequenceFileCreate() 上遇到 -1 错误。并且 -1 未在 MusicSequence 参考中列为错误 - 我是否使用错误的格式来打印 OSStatus?

任何帮助是极大的赞赏!我无法在网上找到很多将 MusicSequence 保存到 .mid 文件的参考资料...

我在最后得到错误。

OSStatus status = 0;

MusicSequence newSeq;
status = NewMusicSequence(&newSeq);
if(status){
    printf("Error new sequence: %ld\n", status);
    status = 0;
} else {
    MusicSequenceSetSequenceType(newSeq, kMusicSequenceType_Seconds);
}

MusicTrack tempoTrack;
status = MusicSequenceGetTempoTrack(newSeq, &tempoTrack);
if(status){
    printf("Error getting tempo track: %ld\n", status);
    status = 0;
}
status = MusicTrackNewExtendedTempoEvent(tempoTrack, 0, 120);
if(status){
    printf("Error adding tempo to track: %ld\n", status);
    status = 0;
}

MusicTrack thisTrack;
status = MusicSequenceNewTrack(newSeq, &thisTrack);
if(status){
    printf("Error adding main track: %ld\n", status);
    status = 0;
}

for(int i = 0; i<convertThis.melodyPoints.count; i++) {
    MIDINoteMessage thisMessage;
    thisMessage.note = thisNote.midiNoteNumber;
    thisMessage.duration = thisNote.duration;
    thisMessage.velocity = 120;
    thisMessage.releaseVelocity = 0;
    thisMessage.channel = 1;
    status = MusicTrackNewMIDINoteEvent(thisTrack, thisNoteBegin, &thisMessage);
    if(status){
        printf("Error adding midi note: %ld\n", status);
        status = 0;
    }
}

NSURL *thisurl = [NSURL URLWithString:[@"~/Documents" stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mid", convertThis.title]]];
status = MusicSequenceFileCreate(newSeq, (__bridge CFURLRef) thisurl, kMusicSequenceFile_MIDIType, kMusicSequenceFileFlags_EraseFile, 0);
if(status != noErr){
    printf("Error on create: %ld\n", status);
    status = 0;
}
4

1 回答 1

3

像这样访问您的文档目录

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

如果您想在其中创建一个文件夹来存储您的 MIDI,您需要先检查它是否有效或像这样创建它 在 iOS 应用程序的文档文件夹中创建一个文件夹

并将字符串添加到 CFURLRef 以直接在 MusicSequenceFileCreate 中使用

NSString *midiPath = [documentsDirectory
                               stringByAppendingPathComponent:[NSString stringWithFormat:@"/MIDIs/%@.mid",convertThis.title]];
CFURLRef midiURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:midiPath];

那么就

status = MusicSequenceFileCreate(newSeq, midiURL, kMusicSequenceFile_MIDIType, kMusicSequenceFileFlags_EraseFile, 0);
于 2012-08-02T18:33:27.950 回答