使用长按手势与 AVFoundation 开始停止录制
Posted
技术标签:
【中文标题】使用长按手势与 AVFoundation 开始停止录制【英文标题】:Use long press gesture with AVFoundation to start stop recording 【发布时间】:2015-04-28 15:32:25 【问题描述】:您好,我想通过长按手势在 Objective-C 中使用 AVFoundation 编码开始停止录制。
我知道如何使用另一个按钮开始录制和停止录制,但不会全部使用同一个按钮。
谢谢!
【问题讨论】:
【参考方案1】:我在 github 中更改了您的代码,并在下面发布了代码。我添加UIGestureRecognizerDelegate
的原因是因为否则您的PLAYBUTTON
s 点击会因为长按手势而被忽略。为了避免延迟,我将[gesture1 setMinimumPressDuration:0];
更改为 0 秒。
当我运行该项目时,它可以按照您的要求完美运行:) 这是.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
这里是实现:
//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
【讨论】:
哇!!这很棒!!非常感谢!! :D 我现在正试图将长按集中在一个新的“记录”按钮上,我会及时通知您应用程序的进展情况! 好的,我在情节提要中添加了一个按钮,我在 .h 中添加了一个属性 recordButton,我将属性与按钮链接。添加 recordButton 以在 .m 中合成。在长按实现中,我更改了 longPress 并使用 sender 来播放按钮,并添加了这一行 recordButton = (UIButton*)sender.view;按钮可以工作,但长按也可以在按钮外工作:(我应该使用容器视图吗? 我还在 viewDidLoad [recordButton addGestureRecognizer:gesture1] 中更改/添加了这些行; [self.view addSubview:recordButton]; nice :) 如果有任何问题我会尽力提供帮助。保重我现在要睡觉了:D【参考方案2】:你可以像这样在里面设置一个UILongPressGestureRecognizer
- (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];
当用户按下视图至少 1 秒时,该函数被触发:
-(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];
而不是您使用 UIButton 的旧方式:
-(IBAction) buttonPressed: (id)sender
NSLog(@"button press detected");
//[self startRecording];
【讨论】:
谢谢,我不确定,当我停止按下按钮时会停止录制吗?我是这种开发的新手 你完全正确。我对 longPressed 方法做了一个小的修改。你能试试并给出反馈吗?这样,当用户开始长按 1 秒时,录音开始,当用户移开手指或来电时,录音停止 如果这可行并且您不希望在开始录制之前有 1 秒延迟,我将更改答案以使用UITapGestureRecognizer
与当前解决方案结合使用
顺便说一句,“当我停止按下按钮时”。您是否将手势识别器添加到按钮?使用我的解决方案,您无需触摸按钮。您可以长按屏幕上的任意位置。注意:提供一些代码会对我们有所帮助
有没有办法在评论框外分享代码,因为太长了?以上是关于使用长按手势与 AVFoundation 开始停止录制的主要内容,如果未能解决你的问题,请参考以下文章