xcode:如何使用 AVFoundation 录制音频后保存音频文件
我浏览了与该主题相关的各种帖子,但答案并没有真正帮助。我使用这个教程来实现音频文件的录制和播放。似乎缺少的是如何永久保存记录。当我退出应用程序时,声音文件就在那里,但里面什么也没有。我什至不知道它是在保存 rocerd 还是只是创建文件。 这是一个代码示例:
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"tmpSound.caf"];
tempRecFile = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
recorder = [[AVAudioRecorder alloc] initWithURL:tempRecFile settings:recSettings error:nil];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
我在此处看到了此问题的可能解决方案,但由于我是 iOS 新手,所以我没有真的明白了,否则它对我不起作用。
请帮助并提供代码示例。我阅读了有关 AVFoundation 和其他类的文档,但没有得到积极的结果。
SOS :)
更新 这是我的录制方法:
-(IBAction)recording{
//***** METHOD for recording
if(isNotRecording){
isNotRecording = NO;
[recButton setTitle:@"Stop" forState:UIControlStateNormal];
recStateLabel.text = @"Recording";
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
}
else{
isNotRecording = YES;
[recButton setTitle:@"Rec Begin" forState:UIControlStateNormal];
playButton.hidden = NO;
recStateLabel.text = @"Not recording";
[recorder stop];
}
}
这是我的 viewDidLoad:
- (void)viewDidLoad
{
//record sound test code - start
isNotRecording = YES;
//playButton.hidden = YES;
recStateLabel.text = @"Not recording";
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
//record sound test code - END
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// code for generating the sound file that is to be saved
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"sound.caf"];
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;
recorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[recorder prepareToRecord];
}
}
这是我的播放方法:
-(IBAction)playback{
//AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil];
NSError *error;
// setting the uri of the formed created file
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"sound.caf"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundFileURL error:&error];
[player setNumberOfLoops:0];
[player prepareToPlay];
[player setVolume:1];
[player play];
NSLog(@"URL je: %@", soundFileURL);
//method 2
if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else
{
[player play];
if(player.isPlaying==YES)
{
NSLog(@"It's playing");
}
}
}
我希望这能让我清楚地知道我在做什么。 有什么建议我可能错了吗?
I browsed through all kinds of post related to this topic but the answers just do not really help. I used this tutorial to implement recording of audio files and playback. What seems to be missing is how to save the record permanently. When I exit my app the sound file is there but nothing is in it. I don't even know if it is saving the rocerd or just creating the file.
Here is a code sample:
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"tmpSound.caf"];
tempRecFile = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
recorder = [[AVAudioRecorder alloc] initWithURL:tempRecFile settings:recSettings error:nil];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
I saw a possible solution to this problem here but since I'm new to iOS I do not really get it or it is just does not working for me.
Please help and provide a code example. I read the documentation about AVFoundation and other classes but with no positive results.
SOS :)
UPDATE
This is my recording method:
-(IBAction)recording{
//***** METHOD for recording
if(isNotRecording){
isNotRecording = NO;
[recButton setTitle:@"Stop" forState:UIControlStateNormal];
recStateLabel.text = @"Recording";
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
}
else{
isNotRecording = YES;
[recButton setTitle:@"Rec Begin" forState:UIControlStateNormal];
playButton.hidden = NO;
recStateLabel.text = @"Not recording";
[recorder stop];
}
}
This is my viewDidLoad:
- (void)viewDidLoad
{
//record sound test code - start
isNotRecording = YES;
//playButton.hidden = YES;
recStateLabel.text = @"Not recording";
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
//record sound test code - END
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// code for generating the sound file that is to be saved
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"sound.caf"];
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;
recorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[recorder prepareToRecord];
}
}
This is my playBack method:
-(IBAction)playback{
//AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil];
NSError *error;
// setting the uri of the formed created file
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"sound.caf"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundFileURL error:&error];
[player setNumberOfLoops:0];
[player prepareToPlay];
[player setVolume:1];
[player play];
NSLog(@"URL je: %@", soundFileURL);
//method 2
if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else
{
[player play];
if(player.isPlaying==YES)
{
NSLog(@"It's playing");
}
}
}
I hopše this makes it clear what I do.
Any suggestions where I might be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要停止记录才能将数据永久保存到文件中:
You need to stop recording in order to save the data to the file permanently:
问题是当你告诉XCode(在viewDidLoad方法中)[recorder prepareToRecord];它会删除以前录制的音频文件。删除“if”语句中的那段代码,当您退出并返回应用程序时,您的音频文件将被保存。
The problem is that when you tell XCode (in the viewDidLoad method) to [recorder prepareToRecord]; it gets rid of the previously recorded audio file. Eliminate that piece of code in the "if" statement and your audio file WILL be saved when you exit and come back in to the app.
仅供参考——“[录音机prepareToRecord]”方法创建一个新的音频文件,覆盖任何以前录制的文件。
FYI -- The "[recorder prepareToRecord]" method creates a fresh audio file, overwriting any previous recorded file.