Reputation: 47
Hi I would like to start stop a recording with AVFoundation coding in Objective-C with long press gesture.
I know how to start a recording and stop it with another button, but not doing it all with the same one.
Thanks!
Upvotes: 0
Views: 1640
Reputation: 1444
I changed your code in github very little and posted the code below. The reason i added the UIGestureRecognizerDelegate
is because otherwise your PLAYBUTTON
s clicks are ignored because of the long press gesture. And i changed [gesture1 setMinimumPressDuration:0];
to 0 seconds for avoiding a delay.
When I run the project it works perfectly as you have asked:) Here is the .h
//viewcontroller.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
@interface ViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate, UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *playButton;
- (void)longPressed:(UIGestureRecognizer *)longPress;
- (IBAction)playTapped:(id)sender;
@end
Here is the implementation:
//viewcontroller.m
#import "ViewController.h"
@interface ViewController ()
{
AVAudioRecorder *recorder;
AVAudioPlayer *player;
}
@end
@implementation ViewController
@synthesize playButton;
- (void)viewDidLoad
{
UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
gesture1.delegate = self;
[gesture1 setMinimumPressDuration:0];
[self.view addGestureRecognizer:gesture1];
[super viewDidLoad];
// Disable Stop/Play button when application launches
[playButton setEnabled:NO];
// Set the audio file
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"MyAudioMemo.m4a",
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
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:nil];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)longPressed:(UIGestureRecognizer *)longPress {
if(([longPress state] == UIGestureRecognizerStateEnded) || ([longPress state] == UIGestureRecognizerStateEnded)) {
NSLog(@"long press ended");
[recorder stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
}
else if([longPress state] == UIGestureRecognizerStateBegan) {
NSLog(@"long press detected");
if (player.playing) {
[player stop];
}
if (!recorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
// Start recording
[recorder record];
}
}
}
- (IBAction)playTapped:(id)sender {
NSLog(@"playTapped");
if (!recorder.recording){
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
[player setDelegate:self];
[player play];
}
}
#pragma mark - AVAudioRecorderDelegate
- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{
NSLog(@"audioRecorderDidFinishRecording");
[playButton setEnabled:YES];
}
#pragma mark - AVAudioPlayerDelegate
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Done"
message: @"Finish playing the recording!"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// test if our control subview is on-screen
if (playButton.superview != nil) {
if ([touch.view isDescendantOfView:playButton]) {
// we touched our control surface
return NO; // ignore the touch
}
}
return YES; // handle the touch
}
@end
Upvotes: 2
Reputation: 1444
You can setup a UILongPressGestureRecognizer
simply like this inside
- (void) viewDidLoad {....}
UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
//[gesture1 setDelegate:self]; you won't need this most probably if detecting the long press is your only expectation from the gesture recognizer
[gesture1 setMinimumPressDuration:1];
[self addGestureRecognizer:gesture1];
//instead of [self addGest... you can try [self.view addGest.. as follows:
[self.view addGestureRecognizer:gesture1];
and when the user presses the view for at least 1 seconds this function is fired:
-(void)longPressed:(UIGestureRecognizer *)longPress {
if(([longPress state] == UIGestureRecognizerStateEnded) || ([longPress state] == UIGestureRecognizerStateEnded)) {
NSLog(@"long press ended");
//[self stopRecording];
}
else if([longPress state] == UIGestureRecognizerStateBegan) {
NSLog(@"long press detected");
//[self startRecording];
}
}
instead of the old way you were using with the UIButton:
-(IBAction) buttonPressed: (id)sender {
NSLog(@"button press detected");
//[self startRecording];
}
Upvotes: 0