0

デフォルトのサウンドがいくつかあり、ユーザーが必要に応じて他のサウンドを追加できる Mac OS X アプリを作成しようとしています。の配列にサウンドをロードしています-awakeFromNib:

for (NSString *str in [PreferencesController beats]) {
    resourcePath = [[NSBundle mainBundle] pathForSoundResource:str];
    beat = [[NSSound alloc] initWithContentsOfFile:resourcePath byReference:YES];
    [beat setLoops:YES];
    [beat setName:str];
    [beatsArray addObject:beat];
}

アプリがユーザーによって追加されたサウンドを配列に追加しようとするまで、すべてが正常に機能します。それは言います:*** -[NSURL initFileURLWithPath:]: nil string parameter。ファイルの URL が見つからないと推測していますが、ユーザーが次のコードでファイルを選択すると、ファイルをアプリのディレクトリにコピーしています。

if ( [openDlg runModalForTypes:[NSArray arrayWithObjects:@"aif",@"aiff",@"mp3",@"wav",@"m4a",nil]] == NSOKButton)
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    NSString *dataPath = [[NSBundle mainBundle] bundlePath];
    NSLog(@"Datapath is %@", dataPath);
    NSLog(@"Selected Files : %@",[[openDlg URLs] objectAtIndex:0]);

    [fileManager copyItemAtURL: [[openDlg URLs] objectAtIndex:0] toURL: [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]] error:&error];
        NSLog(@"File copied");
    NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:[PreferencesController beats]];
    NSString *fileName = [[[openDlg URL] path] lastPathComponent];
    NSArray *fileNameArray = [fileName componentsSeparatedByString:@"."];
    [newArray addObject:[fileNameArray objectAtIndex:0]];
    NSLog(@"%@",newArray);
    [PreferencesController setBeats:newArray];
    [self awakeFromNib];
    [_tableView reloadData];
}

このコードの何が問題になっていますか?

4

1 に答える 1

-1

バンドル パスを宛先 URL として使用しているだけなので、ファイルをフォルダーにコピーしようとしているようです。宛先 URL には、宛先ファイル名を含むフル パスを指定する必要があります。

ファイルがコピーされたときにエラーをログに記録してみてください。

NSLog(@"File copied with error: %@", error);

この行には次のエラーが含まれています。

[fileManager copyItemAtURL: [[openDlg URLs] objectAtIndex:0] toURL: [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]] error:&error];

具体的には、宛先 URL がフォルダーであることが問題です。

[[NSBundle mainBundle] bundlePath]

次のようになります。

[[[NSBundle mainBundle] bundlePath] stringByAppendingString:[[[[openDlg URLs] objectAtIndex:0] path] lastPathComponent];

次に、@Abizern がコメントで述べているように、機能したら、バンドルに保存することはお勧めできません (アプリの一部です)。動作したら、サウンドを保存するためのより適切な場所を選択する必要があります (アプリのサポート ドキュメント フォルダー、プログラムでアプリケーション サポート フォルダーへのパスを取得するなど) 。

于 2013-05-03T12:06:44.517 に答える