使用数组,将声音加载到iTunes的应用程序中

时间:2011-11-05 17:23:13

标签: objective-c ios xcode cocoa-touch itunes

我正在尝试使用iTunes文件共享文件共享多个文件。

这是当前的代码。

Delegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // file sharing trying
    {
        NSString *fileName = @"Test.mp3";
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:fileName];

        if (![fileManager fileExistsAtPath:documentDBFolderPath])
        {
            NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
            [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
        }
    }

    [self.window makeKeyAndVisible];

    return YES;
}

目前只有1个声音文件被共享。

由于

2 个答案:

答案 0 :(得分:0)

将每个文件名放入NSArray中:

NSArray filesToShare = [NSArray arrayWithObjects:@"Test1.mp3",@"Test2.mp3",@"Test3.mp3",@"Test4.mp3",@"Test5.mp3",nil];

for (NSString *filename in filesToShare)
{
    ... Your existing code goes here, 
}

显然,您每次都要将相同的部分移动到for循环之前(获取路径,创建文件管理器等)。

答案 1 :(得分:0)

虽然不是您问题的答案,但将歌曲添加到iTunes通常由Scripting Bridge完成。

您应该通过在终端中运行它来生成iTunes.h头文件:

sdef /Applications/iTunes.app | sdp -fh --basename "iTunes"

然后在代码中添加这样的内容:

NSString *file = @"/path/to/test.mp3";
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier: @"com.apple.iTunes"];
iTunesTrack *track = [iTunes add: [NSArray arrayWithObject: [NSURL fileURLWithPath: file]] to: nil];