【问题标题】:iOS - Get a writable path in C++iOS - 在 C++ 中获取可写路径
【发布时间】:2016-09-02 08:55:18
【问题描述】:

我正在用原生 C++ 为 iOS 编写录音机,但我无法使用 Foundation API。

我需要这样的东西:

recordFilePath = 
(CFStringRef)[NSTemporaryDirectory() stringByAppendingPathComponent: @"recordedFile.caf"];

在 iPhone 上获得可写路径。

非常感谢。

【问题讨论】:

  • 你能解释一下为什么不能使用 Foundation API,如果它打算在 iPhone 上运行吗?
  • 因为它必须用 Cocos2d-x 编译。
  • 我想这就是我想要的,如果仍然有效的话:stackoverflow.com/questions/13469342/…
  • 你听说过 Objective-C++ 吗?您可以通过将文件设置为 .mm 扩展名来混合 Objective-C (so Foundation) 和 C++ (Cocos2d-x)。在您的情况下,您可以在此 .mm 文件中使用一些实用程序功能,例如 resourcePath() 并在 cpp 中使用它们而不会出现任何问题。否则,您仍然可以使用纯 C 的 CoreFoundation。Foundation 几乎不是 CoreFoundation 的 Objective-C 包装器(有一些添加)。
  • 感谢您的回答,我知道了。但正如我所说,C++ 类必须是独立的,并且不应该从 .mm 调用任何外部函数——我认为如果我使用 getenv("HOME") 我可以在没有任何 Foundation 参考的情况下实现我想要的。不过,谢谢你的代码,你很友善。

标签: c++ ios native audioqueue


【解决方案1】:

当然可以,但自信地说,这是我的一个客户的任务,他不要求任何外部功能或桥梁或任何涉及 Foundation 的核心课程。所以我不得不在不引用 Foundation 的情况下构建一个音频队列组件。

为了记录,我成功地这样做了:

        const char *home = getenv("HOME");
        const char *subdir = "/Documents/";
        const char *file = "recordedFile.caf";

        char *recPath = (char*)(calloc(strlen(home) + strlen(subdir) 
                              + strlen(file) + 1, sizeof(char)));

        strcpy(recPath, home); // copy string one into the result.
        strcat(recPath, subdir); // append string two to the result.
        strcat(recPath, file); // append string three to the result.

        //recordFilePath is our CFStringRef
        recordFilePath = CFStringCreateWithCString(0, recPath, kCFStringEncodingUTF8);

        //recorder is an AQRecorder class like the one from SpeakHere code sample
          recorder->StartRecord(recordFilePath);

【讨论】:

    【解决方案2】:

    注释OP后,一个使用Objective-C++的例子:

    Utils.hpp:

    #pragma once
    #include <string>
    
    std::string temporaryFilePath(const std::string &filename)
    

    Utils.mm:

    #import "Utils.hpp"
    #import <Foundation/Foundation.h>
    
    std::string temporaryFilePath(const std::string &filename)
    {
        NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithUTF8String:filename.c_str()]];
        const char *rawFilePath = [filePath UTF8String];
        return rawFilePath;
    }
    

    WhereverYouWant.cpp:

    #include "Utils.hpp"
    
    ...
    
    std::string recordFilePath = temporaryFilePath("recordedFile.caf");
    

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 2019-09-28
      • 2016-03-31
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多