所以我在iPhone(iOS7 / iPhone 5s)上遇到了一些奇怪的东西。
基本上我正在做的是当应用程序将在后台启动录制时会发生事件。一切正常,我已经测试过了。但是,我想给用户一个本地通知,所以如果他们有振动/声音启用,他们可以听到它已经开始录音。
但是,当我这样做时,似乎通知被录音切断了,录音甚至都没有开始。
以下是我的代码的一些部分。如果您需要更多,请告诉我。
在ViewDidLoad
中 AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
[session setActive:YES error:nil];
// Start recording
[self setupRecording];
- (void)setupRecording {
//////////////// GET AUDIO STUFF SET UP ///////////////////////
// Set the audio file
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath_ = [searchPaths objectAtIndex: 0];
NSString *pathToSave = [documentPath_ stringByAppendingPathComponent:@"TEMP.m4a"];
// File URL
NSURL *url = [NSURL fileURLWithPath:pathToSave];//FILEPATH];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:nil];
recorder.delegate = self;
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
//////////////////////////////////////////////////////////////////////////////////////////////
}
- (void)localNotification {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:@"Tapadoodledo is now recording."];
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
- (void)record {
self.startDate = [NSDate date];
self->timeRecording = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
[self.recordingIndicatorButton setTitle:@"Stop recording" forState:UIControlStateNormal];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
[session setActive:YES error:nil];
[recorder record];
NSTimer *TimerUpdate = [NSTimer scheduledTimerWithTimeInterval:.1
target:self selector:@selector(timerTask) userInfo:nil repeats:NO];
[self setRecording];
}
- (void)stopRecording {
[self.recordingIndicatorButton setTitle:@"Record" forState:UIControlStateNormal];
[self->timeRecording invalidate];
[longTimer invalidate];
self.timeRecordingLabel.text = @ "";
// Stop recording
[recorder stop];
[self moveRecordingToCorrectTime];
[self setListening];
}
以下是事件发生时在后台调用的代码:
- (void)longTimer:(NSTimer *)timer1
{
if (count >= 2) {
if (self->recording) {
// [self localNotification];
// [self stopRecording];
[self localNotification];
} else {
// [self localNotification];
// [self record];
}
}
first = YES;
count = 0;
}
我发现如果我评论[自我记录],本地通知工作完美,如果我注释掉本地通知,录音机工作完美,但当两者在一起时我遇到问题(并不总是记录)
我注意到有时zibrate在调用记录时不会发生如果我有它调用记录功能但是如果我删除了对记录的调用它会振动。但它总是为停止录音而振动,即使我有停止录音的调用。
任何帮助都会很棒!
感谢。
答案 0 :(得分:0)
很奇怪,但是我做了以下操作并且“有效”。虽然我仍然很好奇为什么会这样做。
[self localNotification];
double delayInSeconds = 0.2;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self record];
});