gpt4 book ai didi

iphone - xcode:如何在使用 AVFoundation 录制音频后保存音频文件

转载 作者:行者123 更新时间:2023-12-01 15:54:26 26 4
gpt4 key购买 nike

我浏览了与该主题相关的各种帖子,但答案并没有真正帮助。我用了this实现音频文件录制和播放的教程。似乎缺少的是如何永久保存记录。当我退出我的应用程序时,声音文件在那里但没有任何内容。我什至不知道它是在保存 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];

我看到了这个问题的可能解决方案 here但由于我是 iOS 的新手,所以我并不真正了解它,或者它对我不起作用。

请帮助并提供代码示例。我阅读了有关 AVFoundation 和其他类的文档,但没有得到积极的结果。

紧急求救:)

更新这是我的记录方法:

-(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");
}
}

我希望这能让我清楚地知道我在做什么。有什么我可能错的建议吗?

最佳答案

您需要停止记录才能将数据永久保存到文件中:

[recorder stop];

关于iphone - xcode:如何在使用 AVFoundation 录制音频后保存音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9387237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com