gpt4 book ai didi

ios7 - 使用 iOS 7 在 iPhone 5s 上写入/dev/null 时,AVAudioRecorder 停止工作

转载 作者:行者123 更新时间:2023-12-04 23:53:12 25 4
gpt4 key购买 nike

我有一个应用程序可以监控背景音频电平而不记录到文件。我使用写入/dev/null 的技巧来完成此操作。

此代码适用于运行 iOS 6 的 iPhone 3GS、运行 iOS 6 和 iOS 7 的 iPhone 4,以及运行 iOS 7 和 iPhone Retina(4 英寸 64 位)的模拟器。

然而,当我在真正的 iPhone 5s 上尝试时,录音机似乎捕捉了片刻音频,然后无声无息地消失了。

这是代码:

    // Inititalize the audio

NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];


NSError *error;

recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
// peakLevelTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector: @selector(peakLevelTimerCallback:) userInfo: nil repeats: YES];
} else {
NSLog(@"There was an error setting up the recorder.");
NSLog([error description]);
}

任何想法可能会发生什么?

有人可以建议解决方法吗?写入真实文件有效,但我不想为了监视音频而填充设备上的任何空间。有没有办法写入一个刚刚蒸发到空气中的小文件缓冲区?有效地实现我自己的/dev/null 吗?

最佳答案

在为 iOS 7 测试我的一个应用程序时,我遇到了同样的问题。

我通过创建一个初始文件来解决/dev/null 缺失的问题,然后在录制开始几秒钟后将其删除。录音仍然继续适用于我的应用程序,并且没有存储数据文件。

- (void)startRecording
{
// New recording path.
NSString *recorderFilePath = [NSString stringWithFormat:@"%@/%@.caf", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"], @"cache"];
NSURL *url = [NSURL fileURLWithPath:recorderFilePath];

AVAudioRecorder recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];

if([recorder prepareToRecord])
{
[recorder record];
NSTimer deleteFileTimer = [NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(removeRecordingFile) userInfo:nil repeats:NO];
}
}

- (void)removeRecordingFile
{
// Remove the data file from the recording.
NSString *recorderFilePath = [NSString stringWithFormat:@"%@/%@.caf", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"], @"cache"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;

BOOL success = [fileManager removeItemAtPath:recorderFilePath error:&error];

if(success)
{
NSLog(@"Deleted recording file");
}
else
{
NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
}
}

关于ios7 - 使用 iOS 7 在 iPhone 5s 上写入/dev/null 时,AVAudioRecorder 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19417954/

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