iOSに音声を録音して再生したい。また、記録したファイルをサーバーに送信したい。記録されたファイルはANDROIDデバイスでも再生する必要があるという1つのひねりがあります。そこで MP3 を使ってみました。ただし、AVAudioRecorder では、MP3 形式での録音は許可されていません。だから私はAACを使用しています。しかし、それは機能していません。これが私のコードです
-(void)initializeAudioRecorder {
self.btnPlayRecording.enabled = NO;
self.btnStopRecording.enabled = NO;
// Get sound file URL in which recording is saved
NSURL *soundFileURL = [NSURL fileURLWithPath:[self soundFilePath]];
// Set settings for audio recording
NSDictionary *audioRecordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],AVEncoderBitRateKey,
[NSNumber numberWithInt:2], AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],AVSampleRateKey,
[NSNumber numberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey, nil];
NSError *error = nil;
self.audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:audioRecordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
}
else {
if (![self.audioRecorder prepareToRecord]) {
NSLog(@"Failed to prepare recording");
}
}
}
#pragma mark - Get Sound File Path
-(NSString*)soundFilePath {
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDirPath = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDirPath stringByAppendingPathComponent:@"testSound.aac"];
return soundFilePath;
}
#pragma mark - IBAction Methods
- (IBAction)btnStartRecordingTapped:(id)sender {
if (!self.audioRecorder.recording) {
self.btnPlayRecording.enabled = NO;
self.btnStopRecording.enabled = YES;
[self.audioRecorder record];
}
}
- (IBAction)btnStopRecordingTapped:(id)sender {
self.btnStopRecording.enabled = NO;
self.btnPlayRecording.enabled = YES;
self.btnStartRecording.enabled = YES;
}
- (IBAction)btnPlayRecordingTapped:(id)sender {
if (!audioRecorder.recording) {
self.btnStopRecording.enabled = YES;
self.btnStartRecording.enabled = NO;
NSError *error = nil;
self.audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:self.audioRecorder.url error:&error];
self.audioPlayer.delegate = self;
if (error) {
NSLog(@"Error occured while playing recorded audio.\nError:- %@",[error localizedDescription]);
}
else {
[self.audioPlayer play];
}
}
}
#pragma mark - AVAudioPlayer Delegate MEthods
/* audioPlayerDidFinishPlaying:successfully: is called when a sound has finished playing. This method is NOT called if the player is stopped due to an interruption. */
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"In %s",__PRETTY_FUNCTION__);
self.btnStartRecording.enabled = YES;
self.btnStopRecording.enabled = NO;
}
/* if an error occurs while decoding it will be reported to the delegate. */
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
NSLog(@"In %s",__PRETTY_FUNCTION__);
NSLog(@"audioPlayerDecodeError:- %@",[error localizedDescription]);
}
#pragma mark - AVAudioRecorder Delegate MEthods
/* audioRecorderDidFinishRecording:successfully: is called when a recording has been finished or stopped. This method is NOT called if the recorder is stopped due to an interruption. */
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
NSLog(@"In %s",__PRETTY_FUNCTION__);
}
/* if an error occurs while encoding it will be reported to the delegate. */
- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error
{
NSLog(@"In %s",__PRETTY_FUNCTION__);
NSLog(@"audioRecorderEncodeError:- %@",[error localizedDescription]);
}
しかし、上記のコードはオーディオを録音していません。ファイルは DOCS ディレクトリに作成されます。しかし、再生しようとすると、うまくいきません。何も聞こえません。
私のコードで何が問題なのですか? また、iOS + Android で再生される形式でオーディオを録音および再生するためのライブラリはありますか? ありがとう