gg1*_*g13 7 iphone audio core-data objective-c ios
编辑:想出来!我正在保存URL的absoluteString,当我尝试初始化音频播放器时,我使用了[NSURL fileURLWithPath:name]; 什么时候它应该是[NSURL urlWithString:name].通过创建另一个fileURLWithPath,它正在改变路径并且不起作用.我会留下一些示例代码我猜!
好的,所以我在我的应用程序中有一个用户录制的音频片段.我希望用户能够保存这些录音,所以我想我应该将录音的网址字符串保存到核心数据中,获取网址,并使用该网址加载AVAudioPlayer.但它不起作用.
它给了我:
Error in audioPlayer: The operation could not be completed.
Run Code Online (Sandbox Code Playgroud)
这是URL的NSString:
file://localhost/file:/localhost/var/mobile/Applications/E09081DE-434F-46CA-A598-
A2470973C0F1/Documents/Rang%2520De%2520sound.caf
Run Code Online (Sandbox Code Playgroud)
我有一种感觉它与它保存在localhost中的事实有关,而我在尝试初始化音频播放器时实际上无法访问该文件.但我不确定.我知道可以立即访问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!";
}
Run Code Online (Sandbox Code Playgroud)
当我获取它时,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
Run Code Online (Sandbox Code Playgroud)
它从Le%20变为Le%2520!我没有重新录制或任何我只是在核心数据中保存相同的字符串并获取它!开头的localhost也有一些变化.
那么这就是我如何获取它:
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];
Run Code Online (Sandbox Code Playgroud)
有关如何解决这些家伙的任何帮助?如果它与localhost有关,那么如何将其保存在其他地方呢?
这是保存位置的代码:
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];
}
Run Code Online (Sandbox Code Playgroud)
很抱歉这个问题的长度,只是希望尽可能有用,并提出任何代码可能有助于解决问题.让我知道这是否是我做错了的其他事情!多谢你们!
弄清楚了!我正在保存absoluteStringURL,当我尝试初始化我[NSURL fileURLWithPath:name]应该使用的音频播放器时[NSURL URLWithString:name].通过创建另一个,fileURLWithPath它正在改变路径,不会起作用.我会留下一些示例代码我猜!
| 归档时间: |
|
| 查看次数: |
2435 次 |
| 最近记录: |