Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's how I did it:</p> <p>1) Find the temp file that the SpeakHere code creates -- look for the .caf extension in the SpeakHereController class. Then move that temp file to your application directory with something like this:</p> <pre><code>NSString *myFileName = @"MyName"; // this would probably come from a user text field NSString *tempName = @"recordedFile.caf"; NSString *saveName = [NSString stringWithFormat:@"Documents/%@.caf", myFileName]; NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:tempName]; NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:saveName]; </code></pre> <p>2) Save some metadata about the file, at least its name. I'm putting that into NSUserDefaults like this:</p> <pre><code>NSDictionary *recordingMetadata = [NSDictionary dictionaryWithObjectsAndKeys: myFileName, @"name", [NSDate date], @"date", nil]; [self.savedRecordings addObject:recordingMetadata]; // savedRecordings is an array I created earlier by loading the NSUserDefaults [[NSUserDefaults standardUserDefaults] setObject:self.savedRecordings forKey:@"recordings"]; // now I'm updating the NSUserDefaults </code></pre> <p>3) Now you can display a list of saved recordings by iterating through self.savedRecordings.</p> <p>4) When the user selects a recording, you can easily initialize an AVAudioPlayer with the selected file name and play it back.</p> <p>5) To let users delete recordings, you can do something like this:</p> <pre><code>NSString *myFileName = @"MyName"; // delete the audio file from the application directory NSString *fileName = [NSString stringWithFormat:@"Documents/%@.caf", myFileName]; NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:fileName]; [[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL]; // delete the metadata from the user preferences for (int i=0; i&lt;[self.savedRecordings count]; i++) { NSDictionary *thisRecording = [self.savedRecordings objectAtIndex:i]; if ([myFileName isEqualToString:[thisRecording objectForKey:@"name"]]) { [self.savedRecordings removeObjectAtIndex:i]; break; } } [[NSUserDefaults standardUserDefaults] setObject:self.savedRecordings forKey:@"recordings"]; </code></pre> <p>Note that if you save the audio files into the Documents folder and enable "Application supports iTunes file sharing" in your info.plist, then users can copy their recordings out of the app and save them onto their computers ... a nice feature if you want to offer it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload