7

编辑:想通了!我正在保存 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];
    }

对不起这个问题的长度,只是想尽可能地提供帮助并放置任何可能有助于解决问题的代码。让我知道是否是我做错的其他事情!多谢你们!

4

1 回答 1

8

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

于 2012-08-04T18:21:00.010 回答