【问题标题】:How to set CoreData (NSManagedObject) as destination for AvAudioRecorder?如何将 CoreData (NSManagedObject) 设置为 AvAudioRecorder 的目标?
【发布时间】:2014-07-25 14:14:48
【问题描述】:

我可以通过什么方法将 NSManagedObject 设置为 AVAudioRecorder 的目标(而不是 NSURL)?我想将微小的声音片段保存为二进制核心数据类型,并选择“外部存储”选项......当然可以播放它们。我已经看到了几个绕过这个主题的问题,似乎有一些正在这样做,但我还没有找到任何核心代码示例。到目前为止,我已经按照本教程成功记录了基本文件类型:http://www.supersuraccoon-cocos2d.com/2011/06/05/voice-record-demo/,我只想将其直接用于核心数据。

仅供参考 - 我确实意识到最好的做法是存储 URL,然后检索,但如果可能的话,我真的很想直接将二进制文件放入 CoreData。

谢谢!!

附言如果您有 Swift 的示例,我们将不胜感激!

【问题讨论】:

  • 我认为这是不可能的。如果您看到其他人声称这样做,请在他们讨论的地方发布一些 URL。
  • 感谢您的回复,汤姆。我可能会误解这一点,但这里是一个 URL stackoverflow.com/questions/4649918/…

标签: ios core-data swift avaudiorecorder


【解决方案1】:

您需要将音频保存为 NSData。您需要将文件保存在磁盘中并转换为 NSData。

我有一个配置我的音频的功能,还有一秒转换的功能!!!

-(void)persistDataAudioPlaylet{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,      YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myAudio.caf"];
    NSData *fileContents = [NSData dataWithContentsOfFile:filePath];
    self.playlet.sound = fileContents;
}


-(void)configureAudioRecord{
     NSArray *dirPaths;
     NSString *docsDir;

     dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory, NSUserDomainMask, YES);
     docsDir = dirPaths[0];

     NSString *soundFilePath = [docsDir
                           stringByAppendingPathComponent:@"myAudio.caf"];

     NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

     NSDictionary *recordSettings = [NSDictionary
                                dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:AVAudioQualityMin],
                                AVEncoderAudioQualityKey,
                                [NSNumber numberWithInt:16],
                                AVEncoderBitRateKey,
                                [NSNumber numberWithInt: 2],
                                AVNumberOfChannelsKey,
                                [NSNumber numberWithFloat:44100.0],
                                AVSampleRateKey,
                                nil];

     NSError *error = nil;

     AVAudioSession *audioSession = [AVAudioSession sharedInstance];
     [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
                    error:nil];

     self.audioRecorder = [[AVAudioRecorder alloc]
                  initWithURL:soundFileURL
                  settings:recordSettings
                  error:&error];

    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);
    } else {
        [self.audioRecorder prepareToRecord];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    相关资源
    最近更新 更多