iphone - 记录的url保存在核心数据中,但我稍后尝试使用它时不起作用

标签 iphone objective-c ios core-data audio

编辑:想通了!我正在保存 URL 的 absoluteString,当我尝试初始化我使用的音频播放器时 [NSURL fileURLWithPath:name];当它应该是 [NSURL urlWithString:name] 时。通过创建另一个 fileURLWithPath 它正在更改路径并且不起作用。我想我会把它留给一些示例代码!

好的,我的应用程序中有用户录制的音频片段。我希望用户能够保存这些录音,所以我想我应该将录音的 url 字符串保存到核心数据中,获取 url,并使用该 url 加载 AVAudioPlayer。但它不起作用。

它给了我:

Error in audioPlayer: The operation could not be completed.

这是 URL 的 NSString:

 file://localhost/file:/localhost/var/mobile/Applications/E09081DE-434F-46CA-A598-
A2470973C0F1/Documents/Rang%2520De%2520sound.caf

我感觉它与保存在本地主机中的事实有关,并且在尝试初始化音频播放器时我实际上无法访问该文件。但我不确定。我知道可以立即访问该 URL,因为在我录制之后,我可以使用该 URL 播放录制内容。

我如何保存录音:

- (IBAction)saveRecording 
{
    rapAppDelegate *appDelegate = 
    [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = 
    [appDelegate managedObjectContext];
    NSManagedObject *newSong;
    newSong = [NSEntityDescription
                  insertNewObjectForEntityForName:@"URLRecordings"
                  inManagedObjectContext:context];
    NSError *error;

    NSURL *urlOfRecording = audioRecorder.url;
    NSString *urla = [urlOfRecording absoluteString];
    NSLog(@"in saveRecording, the url is: %@",urla);
    [newSong setValue:urla forKey:@"urlOfSong"];
    [context save:&error];
    status.text = @"Song Saved!";
}

当我获取它时,URL 发生了变化!我不明白为什么!看:

2012-08-03 16:31:17.434 Rap To Beats[5054:707] in saveRecording, the url is: file://localhost/var/mobile/Applications/E09081DE-434F-46CA-A598-A2470973C0F1/Documents/Le%20Gayi%20sound.caf

2012-08-03 16:31:24.381 Rap To Beats[5054:707] after fetch, the url is: file://localhost/file:/localhost/var/mobile/Applications/E09081DE-434F-46CA-A598-A2470973C0F1/Documents/Le%2520Gayi%2520sound.caf

它从 Le%20 变成了 Le%2520!我没有重新录制或任何我只是在核心数据中保存相同的字符串并获取它!开始时本地主机也有一些变化。

这就是我获取它的方式:

rapAppDelegate *appDelegate = 
    [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = 
    [appDelegate managedObjectContext];

    NSEntityDescription *entityDesc = 
    [NSEntityDescription entityForName:@"URLRecordings" 
                inManagedObjectContext:context];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [request setEntity:entityDesc];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:@"urlOfSong" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[context
                                            executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
    }

    [self setUrlArray:mutableFetchResults];

关于如何解决这个问题的任何帮助?如果它与本地主机有关,那么如何将它保存在其他地方?

这是保存位置的代码:

NSArray *dirPaths;
    NSString *docsDir;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *name = self.beatName.text;
    NSString *soundFilePath1 = [docsDir
                               stringByAppendingPathComponent:name];
    NSString *soundFilePath = [soundFilePath1
                               stringByAppendingString:@" sound.caf"];
    NSLog(@"in viewDidLoad, the url is: %@",soundFilePath);
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

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

    NSError *error = nil;

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

    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);

    } else {
        [audioRecorder prepareToRecord];
    }

对于这个问题的长度,我们深表歉意,只是想尽可能提供帮助,并提供任何可能有助于解决问题的代码。如果我做错了什么,请告诉我! 谢谢大家!

最佳答案

想通了!我正在保存 URL 的 absoluteString,当我尝试初始化音频播放器时,我使用了 [NSURL fileURLWithPath:name] 而它应该是 [NSURL URLWithString :名称]。通过创建另一个 fileURLWithPath 它正在更改路径并且不起作用。我想我会把它留给一些示例代码!

关于iphone - 记录的url保存在核心数据中,但我稍后尝试使用它时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11802822/

相关文章:

javascript - 如何在 iPhone 的 Phonegap 应用程序中从 Objective-C 中调用 JavaScript 函数?

ios - 使用 NSAttributedString 进行 AirPrint

ios - Shinobi SChartScatterSeries z 顺序

iphone - 拒绝等待开发者发布的应用程序

ios - 使用 CGRectMake 重绘 UIButton 不会更新 UIButton 位置?

iphone - 使用 mkmap 加载 map 时显示标题

ios - UILabel 为 nil - 无法更新 UILabel 的文本

ios - 有什么可以阻止 -[NSData bytes] 成为悬空指针吗?

ios - Xcode随机变量不起作用

ios - CSS 背景 URL 在 iOS(Chrome 和 Safari)中不起作用,在桌面上正常