我正在創建錄製音頻並將該文件保存在設備中,然後我要發送到服務器。爲此我正在寫這樣的代碼如何將音頻文件保存在覈心數據中併發送到服務器?
_playAudioOutlet.enabled = NO;
_stopRecording.enabled = NO;
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];
NSLog(@"%@",soundFilePath);
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
_audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[_audioRecorder prepareToRecord];
}
// to start recording...
- (IBAction)startRecording:(UIButton *)sender {
if (!_audioRecorder.recording)
{
_playAudioOutlet.enabled = NO;
_stopRecording.enabled = YES;
[_audioRecorder record];
}
}
// To stop recording
- (IBAction)stopRecording:(UIButton *)sender {
_stopRecording.enabled = NO;
_playAudioOutlet.enabled = YES;
_startRecording.enabled = YES;
if (_audioRecorder.recording)
{
[_audioRecorder stop];
} else if (_audioPlayer.playing) {
[_audioPlayer stop];
}
}
// TO play audio
- (IBAction)playAudio:(UIButton *)sender {
if (!_audioRecorder.recording)
{
_stopRecording.enabled = YES;
_startRecording.enabled = NO;
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:_audioRecorder.url
error:&error];
_audioPlayer.delegate = self;
if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else
[_audioPlayer play];
}
}
我正在寫這樣的代碼......它工作正常。但是現在我想將這些數據存儲在覈心數據中,並保存了我想發送的數據之後。爲此,我創建了核心數據和實體名稱:保存,屬性名稱:保存類型二進制數據。但我得到錯誤。 任何人能給出解決方案,這...
- (IBAction)stopRecording:(UIButton *)sender {
// Get conntext
appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
context = appDelegate.persistentContainer.viewContext;
// Save data
NSManagedObject *newSong;
newSong = [NSEntityDescription
insertNewObjectForEntityForName:@"Save"
inManagedObjectContext:context];
NSError *error;
NSURL *urlOfRecording = _audioRecorder.url;
NSString *url = [urlOfRecording absoluteString];
NSLog(@"in saveRecording, the url is: %@",url);
[newSong setValue:url forKey:@"save"];
[context save:&error];
// status.text = @"Song Saved!";
_stopRecording.enabled = NO;
_playAudioOutlet.enabled = YES;
_startRecording.enabled = YES;
if (_audioRecorder.recording)
{
[_audioRecorder stop];
} else if (_audioPlayer.playing) {
[_audioPlayer stop];
}
}
好吧,其實我想複製該文件,並保存與另一個名稱。現在我想發送到服務器。如何重新保存文件... – iOS
這是不正確的。音頻文件可以保存到核心數據。 –
https://stackoverflow.com/questions/14280989/renaming-an-existing-file-with-obj-c – Torongo