gpt4 book ai didi

iphone - AVAudioRecorder 不会在设备上录制

转载 作者:行者123 更新时间:2023-12-03 18:40:41 53 4
gpt4 key购买 nike

这是我的方法:

-(void) playOrRecord:(UIButton *)sender {
if (playBool == YES) {
NSError *error = nil;
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat: @"%d", [sender tag]] ofType:@"caf"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error];
[player setNumberOfLoops:0];
[player play];
}
else if (playBool == NO) {
if ([recorder isRecording]) {
[recorder stop];
[nowRecording setImage:[UIImage imageNamed:@"NormalNormal.png"] forState:UIControlStateNormal];
[nowRecording setImage:[UIImage imageNamed:@"NormalSelected.png"] forState:UIControlStateSelected];
}
if (nowRecording == sender) {
nowRecording = nil;
return;
}
nowRecording = sender;
NSError *error = nil;
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat: @"%d", [sender tag]] ofType:@"caf"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
[sender setImage:[UIImage imageNamed:@"RecordingNormal.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:@"RecordingSelected.png"] forState:UIControlStateSelected];
recorder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:recordSettings error:&error];
[recorder record];
}
}

大部分内容都是不言自明的; playBool 是一个 BOOL,在播放模式下为 YES。一切都在模拟器中运行,但是,当我在设备上运行它时,[记录器记录]返回“否”。有谁知道为什么会发生这种情况吗?

最佳答案

如果您的代码中的任何位置有此设置:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

将其更改为:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

设置错误的AVAudioSession类别会导致录音/播放失败。

关于iphone - AVAudioRecorder 不会在设备上录制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2676292/

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