我正在尝试从项目包之外打开MIDI文件。当文件位于分发包中时,我已经能够成功打开和读取内容。
这是我创建的用于获取文件路径的代码:
从: SetupScene.m
-(void) createFileOpenDialog {
// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];
// Multiple files not allowed
[openDlg setAllowsMultipleSelection:NO];
// Can't select a directory
[openDlg setCanChooseDirectories:YES];
// Display the dialog. If the OK button was pressed,
// process the files.
if ([openDlg runModal] == NSModalResponseOK)
{
// Get an array containing the full filenames of all
// files and directories selected.
NSArray* urls = [openDlg URLs];
// Loop through all the files and process them.
for(int i = 0; i < [urls count]; i++ )
{
_fileNamePath = [urls objectAtIndex:i];
_fileName = [_fileNamePath lastPathComponent];
[_fileTextBox setText:_fileName];
NSLog(@"Url: %@", _fileName);
}
}
}
在另一个类中,我从上面的代码提供的路径中打开文件:
从: MIDIParser.m
//loads the MIDI file with the song title provided by NSUserDefaults
-(void) loadSequence {
NSString *songTitle = [[NSUserDefaults standardUserDefaults] objectForKey:@"songTitle"];
NewMusicSequence(&_sequence);
NSURL *midiFile = [[NSBundle mainBundle] URLForResource:songTitle withExtension:@"mid"];
if (midiFile == nil) { //the song might have been loaded outside of the project bundle
midiFile = [NSURL fileURLWithPath:songTitle];
}
MusicSequenceFileLoad(_sequence, (__bridge CFURLRef)midiFile, 0, 0);
}
在桌面上的文件上尝试此操作时,出现以下错误消息:
322: EXCEPTION (100002): "Open::fopen failed"
ERROR: There are no tracks in this MIDI file.