首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与AVFoundation一起使用长按压手势开始停止录制

问与AVFoundation一起使用长按压手势开始停止录制
EN

Stack Overflow用户
提问于 2015-04-28 15:32:25
回答 2查看 1.6K关注 0票数 0

嗨,我想开始停止与AVFoundation编码的目标-C与长新闻手势录音。

我知道如何开始录音,用另一个按钮来停止它,但不是用同一个按钮。

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-29 18:56:20

我在github中对您的代码做了很小的修改,并在下面发布了代码。我添加UIGestureRecognizerDelegate的原因是,否则您的PLAYBUTTON单击会因为长按手势而被忽略。为了避免延迟,我将[gesture1 setMinimumPressDuration:0];更改为0秒。

当我运行该项目时,它完全按照您的要求工作:)下面是.h

代码语言:javascript
复制
//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

以下是实现:

代码语言:javascript
复制
//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
票数 2
EN

Stack Overflow用户

发布于 2015-04-28 15:45:04

你可以在里面设置一个像这样的UILongPressGestureRecognizer

- (void) viewDidLoad {....}

代码语言:javascript
复制
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秒时,将触发此函数:

代码语言:javascript
复制
-(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中使用的旧方式:

代码语言:javascript
复制
-(IBAction) buttonPressed: (id)sender {
    NSLog(@"button press detected");
    //[self startRecording];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29923659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档