2015-04-28 23 views

回答

1

我在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 
+0

告訴我這個是否按預期工作? – aytunch

+0

哇!太棒了!!非常感謝!:D –

+0

很高興幫助:) – aytunch

0

你可以設置一個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]; 
} 
+0

謝謝,我不知道,當我停止按下按鈕時它會停止錄製嗎?我是新來的這種發展 –

+0

你是完全正確的。我在longPressed方法中做了一個小修改。你可以請嘗試並提供反饋?這種方式,當用戶開始1秒鐘的長時間,記錄開始,當用戶拿走手指或者當有電話來時,記錄停止。 – aytunch

+0

如果這樣工作,並且你不想在開始記錄前1秒延遲,我會改變使用'UITapGestureRecognizer'與當前解決方案結合的答案 – aytunch