録音したオーディオを作成し、サーバーに送信した後にそのファイルをデバイスに保存します。このため私はこのようなコードを書いていますコアデータにオーディオファイルを保存してサーバーに送信する方法は?
_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