如何解决Talking Voice App Microphone 不适用于设备但适用于模拟器/XCode? (iOS 14+)
我有一个会说话的人型应用程序,当你说话时它会重复给你,但是,当我尝试使用 XCode 直接构建到设备时,它工作正常,如果我使用模拟器它工作正常,但是如果我导出 ipa 并安装它,它不起作用......如果我上传到商店,它会因此被拒绝。 声音没有播放,就像麦克风被阻止一样(但事实并非如此,因为我已经设置了启用麦克风访问的权限)。
部分代码;
在我的AppDelegate.m
AVAudioSessionRecordPermission permissionStatus = [[AVAudioSession sharedInstance] recordPermission];
switch (permissionStatus) {
case AVAudioSessionRecordPermissionUndetermined:
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
NSLog(@"req granted");
}
else {
NSLog(@"req denied");
}
}];
break;
case AVAudioSessionRecordPermissionDenied:
NSLog(@"denied");
break;
case AVAudioSessionRecordPermissionGranted:
NSLog(@"granted");
break;
default:
break;
并在代码中;
-(void) initAudioMonitor
{
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
NSString* fullFilePath = [[[NSHomeDirectory() stringByAppendingString:@"/Documents/Audio/"] stringByAppendingString:@"monitor.caf"] retain];
monitorTmpFile = [NSURL fileURLWithPath:fullFilePath];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
// Allow to play in background
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
//AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,// sizeof (audioRouteOverride),&audioRouteOverride);
audioMonitor = [[ AVAudioRecorder alloc] initWithURL: monitorTmpFile settings:recordSetting error:&error];
[audioMonitor setMeteringEnabled:YES];
[audioMonitor setDelegate: self];
[audioMonitor record];
}
-(void) initAudioRecorder
{
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
NSString* fullFilePath = [[[NSHomeDirectory() stringByAppendingString:@"/Documents/Audio/"] stringByAppendingString:@"in.caf"] retain];
inUrl = [NSURL fileURLWithPath:fullFilePath];
recorder = [[ AVAudioRecorder alloc] initWithURL: inUrl settings:recordSetting error:&error];
[recorder setMeteringEnabled:YES];
[recorder setDelegate: self];
[recorder prepareToRecord];
}
-(void)monitorAudio: (ccTime) dt
{
if(!isPlaying)
{
[audioMonitor updateMeters];
// a convenience,it’s converted to a 0-1 scale,where zero is complete quiet and one is full volume
const double ALPHA = 0.05;
double peakPowerForChannel = pow(10,(0.05 * [audioMonitor peakPowerForChannel:0]));
double audioMonitorResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * audioMonitorResults;
//NSLog(@"audioMonitorResults: %f",audioMonitorResults);
// [lab setString:[NSString stringWithFormat:@"--%f",audioMonitorResults]];
AppDelegate *app= (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (audioMonitorResults > AUDIOMONITOR_THRESHOLD)
{ // NSLog(@"Sound detected");
silenceTime = 0;
if(!isRecording)
{
[self startRecording];
[stand setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"ear%d.png",app.animalNo]]];
}
if(isRecording)
{
[stand setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"ear%d.png",app.animalNo]]];
}
}
else
{ // NSLog(@"Silence detected");
if(isRecording)
{ if(silenceTime > MAX_SILENCETIME)
{
if(video==TRUE)
{
}
[stand setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"Turn%d1.png",app.animalNo]]];
NSLog(@"Next silence detected");
[audioMonitor stop];
[self stopRecordingAndPlay];
silenceTime = 0;
} else
{
silenceTime += dt;
}
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。