嗨,我想开始停止与AVFoundation编码的目标-C与长新闻手势录音。
我知道如何开始录音,用另一个按钮来停止它,但不是用同一个按钮。
谢谢!
发布于 2015-04-29 18:56:20
我在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发布于 2015-04-28 15:45:04
你可以在里面设置一个像这样的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];
}https://stackoverflow.com/questions/29923659
复制相似问题