检索创建ios的文件的路径

时间:2014-06-23 08:42:35

标签: ios filenames filepath nsbundle

我有以下用于创建文件的代码:

NSString *name = @"dtmf-received.aif";

//create file
NSString *cachesFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
fileName= [cachesFolder stringByAppendingPathComponent:name];

我需要获取创建文件的路径。以下代码无效。我错过了什么?

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"dtmf-received.aif"
                                                         ofType:@""];

4 个答案:

答案 0 :(得分:0)

caches目录和app bundle目录不同。只需在两个地方使用相同的代码,它就能正常工作。

答案 1 :(得分:0)

Here it is an example how you can play your aif file:


-(void) playSound {
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"dtmf-received" ofType:@"aif"];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
    AudioServicesPlaySystemSound (soundID);
    //[soundPath release];
    NSLog(@"soundpath retain count: %d", [soundPath retainCount]);
}

答案 2 :(得分:0)

试试这个:

        NSString *fileLocation = [[NSBundle mainBundle] pathForResource:@"dtmf-received" ofType:@"aif"];
        NSURL *vedioURL =[NSURL fileURLWithPath:fileLocation];
        MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
        [self presentMoviePlayerViewControllerAnimated:videoPlayerView];
        [videoPlayerView.moviePlayer play];

答案 3 :(得分:0)

您需要将.aif设置为'Type:'string

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"dtmf-received.aif"
                                                     ofType:@""];

试试这个 - >

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"dtmf-received"
                                                     ofType:@"aif"];

并检查项目'Copy Bundle Resources'部分,你的文件需要在这里

Copy Bundle Resouces