ios - 使用长按手势与 AVFoundation 开始停止录制

标签 ios objective-c avfoundation long-press

您好,我想通过长按手势在 Objective-C 中使用 AVFoundation 编码开始停止录制。

我知道如何开始录制并使用另一个按钮停止录制,但不知道如何使用同一个按钮完成所有操作。

谢谢!

最佳答案

我在 github 中对您的代码进行了少量更改,并在下面发布了代码。我添加 UIGestureRecognizerDelegate 的原因是因为长按手势会忽略您的 PLAYBUTTON 点击。我将 [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

关于ios - 使用长按手势与 AVFoundation 开始停止录制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29923659/

相关文章:

Iphone用户输入结构

ios - Swift 3 核心数据

ios - 想知道我的 Xcode 项目中是否存在使用 objective-c 的目录?

objective-c - 使用 Cocoa 或 Applescript 获取 OSX 桌面上图标的大小

objective-c - AVFoundation 重现视频循环

ios - 如何在 UITableViewCell 中添加交互式 View Controller ?

ios - 如何在iOS中共享图像,消息和URL(Objective-C)

objective-c - Nib 中的 2 个表格 View

android - 在 Android 上播放的 iOS 上的音频文件听不到我的声音。只有声音不好

ios4 - 如何从 AVCaptureAudioDataOutput 播放音频样本缓冲区