【问题标题】:retrieve path of a file created ios检索创建的文件的路径 ios
【发布时间】:2014-06-23 08:42:35
【问题描述】:

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

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:@""];

【问题讨论】:

    标签: ios filenames filepath nsbundle


    【解决方案1】:

    缓存目录和应用程序包目录不同。只需在两个地方使用相同的代码,它就会正常工作。

    【讨论】:

    • 使用相同的代码是什么意思?能否请您添加示例?
    • 第一段代码是正确的,所以到处使用它。
    • 我已经改变了这个 NSString* filePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] pathForResource:@"dtmf-received.aif" ofType:@"aif"];现在我崩溃了。
    • 我不明白你为什么觉得这很困难。该代码与第一段代码不同,是吗?使用相同的代码。
    • 所以我可以使用 filePath 作为文件名。好的,谢谢!
    【解决方案2】:
    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]);
    }
    

    【讨论】:

    • 谢谢!你能告诉我如何在 ios 中创建一个 10 562 字节的空文件吗?该文件将通过流打开和流写入打开,我将附加一些数据
    • 这是您需要的确切尺寸吗?
    【解决方案3】:

    试试这个:

            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];
    

    【讨论】:

    • 谢谢!你能告诉我如何在 ios 中创建一个 10 562 字节的空文件吗?该文件将通过流打开和流写入打开,我将附加一些数据。
    【解决方案4】:

    您需要将 .aif 设置为 'Type:' 字符串

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

    试试这个->

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

    并检查项目“复制捆绑资源”部分,您的文件需要在这里

    【讨论】:

    • 感谢您的重播。我还需要创建 dtmf-received.aif。你能帮我在主包中创建文件吗?
    • 将文件拖到“复制捆绑资源”
    • 如何在 ios 中创建特定大小的文件?
    猜你喜欢
    • 2011-10-31
    • 1970-01-01
    • 2018-03-06
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    相关资源
    最近更新 更多