嗨我想開始用長按手勢在Objective-C中使用AVFoundation編碼停止錄製。使用AVFoundation的長按手勢開始停止錄製
我知道如何開始錄製並用另一個按鈕來停止錄製,但不能用同一個按鈕來完成。
謝謝!
嗨我想開始用長按手勢在Objective-C中使用AVFoundation編碼停止錄製。使用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
你可以設置一個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];
}
告訴我這個是否按預期工作? – aytunch
哇!太棒了!!非常感謝!:D –
很高興幫助:) – aytunch