gpt4 book ai didi

ios - iOS,在iPad中没有录制声音,在模拟器中工作正常

转载 作者:行者123 更新时间:2023-12-03 02:21:18 27 4
gpt4 key购买 nike

嗨,我在模拟器中有一个非常奇怪的问题,它可以完美运行,但是当我向iPad充电时,它无法正常工作。任何的想法?

在这里,我创建了所有变量以使其工作。

- (void)viewDidLoad {

[super viewDidLoad];

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;

audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}

}

我有一些用于记录,播放和停止的按钮。因此,这些是所有代码。
-(void) recordAudio{
if (!audioRecorder.recording){
recordButton.enabled = NO;
playButton.enabled = NO;
saveButton.enabled = NO;
stopButton.enabled = YES;
[audioRecorder record];
}
}

-(void)stop{
recordButton.enabled = YES;
playButton.enabled = YES;
saveButton.enabled = YES;
stopButton.enabled = NO;
if (audioRecorder.recording){
[audioRecorder stop];
}
else if (audioPlayer.playing){
[audioPlayer stop];
}
}


-(void) playAudio{
if (!audioRecorder.recording){
recordButton.enabled = NO;
stopButton.enabled = YES;
saveButton.enabled = NO;
if (audioPlayer)
audioPlayer=nil;
NSError *error;

audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:audioRecorder.url
error:&error];

audioPlayer.delegate = self;

if (error)
NSLog(@"Error: %@", [error localizedDescription]);
else
[audioPlayer play];
}
}

最佳答案

终于我找到了解决方案。在我之前的代码中,我在ViewdidLoad中添加了这些行。

AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
[audioSession setActive:YES error: &error];

对于设置,我添加了以下内容:
[NSNumber numberWithInt:kAudioFormatAppleIMA4], AVFormatIDKey

我从 http://www.iphoneam.com/blog/index.php?title=using-the-iphone-to-record-audio-a-guide&more=1&c=1&tb=1&pb=1复制了代码,使用AVAudioSession的想法来自 How to record and play sound in iPhone app?

如果您需要完整的代码,请使用此代码。
- (void)viewDidLoad {

[super viewDidLoad];

NSError *error = nil;

AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
[audioSession setActive:YES error: &error];

playButton.enabled = NO;
stopButton.enabled = NO;
saveButton.enabled = NO;

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
docsDir = [HSAudioController pathToDocumentsFolder];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatAppleIMA4], AVFormatIDKey,
[NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16], AVEncoderBitRateKey,
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
nil];

audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL
settings:recordSettings
error:&error];
NSLog(@"%@",audioRecorder.url);
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}
}

关于ios - iOS,在iPad中没有录制声音,在模拟器中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12311194/

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