編集:それを理解しました!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
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!";
}
フェッチすると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];
}
この質問の長さについて申し訳ありませんが、できるだけ役立つようにし、問題の解決に役立つ可能性のあるコードを入力してください。それが私が間違ってやっている他の何かであるかどうか私に知らせてください!みんなありがとう!