在我的应用中,我想录制语音并通过电子邮件发送。我正在录制语音,一旦我点击附加按钮,它就会引发以下错误。似乎没有录音。
这是我的代码段
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"MyAudioMemo.m4a",
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
NSLog(@"%@",outputFileURL);
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord 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:outputFileURL settings:recordSetting error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
}
- (IBAction)microButton:(id)sender {
if(counter%2==0)
{
if (!recorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
// Start recording
[recorder record];
}
counter=counter+1;
}
else{
[recorder stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
counter=counter+1;
}
}
- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{
NSLog(@"audioPlayerDidFinishPlaying");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Done" message: @"Voice recorded successfully!"delegate: nil cancelButtonTitle:@"OK"otherButtonTitles:nil];
[alert show];
}
- (IBAction)attachBtn:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *URL = [documentsDirectory stringByAppendingPathComponent:@"MyAudioMemo.m4a"];
NSString* someText = @"Voice";
NSURL *urlToShare = [NSURL fileURLWithPath:URL isDirectory:NO];
NSArray* dataToShare = @[someText, urlToShare];
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypePrint,UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact];
activityViewController.popoverPresentationController.barButtonItem = (UIBarButtonItem *)sender;
activityViewController.completionHandler = ^(NSString *activityType, BOOL completed) {
//if (completed) {
[self dismissViewControllerAnimated:YES completion:nil];
//}
};
[self presentViewController:activityViewController animated:YES completion:nil];
}
答案 0 :(得分:1)
假设这是在iPad上,当您呈现UIActivityViewController
时,它将以弹出窗口显示。您需要执行消息所述的操作,设置sourceView
或barButtonItem
。
activityViewController.popoverPresentationController.barButtonItem = sender;
假设sender
是UIBarButtonItem
。如果是UIButton
或其他视图,请使用:
activityViewController.popoverPresentationController.sourceView = sender;