2014-11-14 146 views
0

代码转储道歉,但我不能发现有什么问题。 应用程序是一个基本的记录,暂停,本教程发挥使用AVFoundation:http://www.appcoda.com/ios-avfoundation-framework-tutorial/iOS AVFoundation/AVAudioRecorder未知崩溃

ViewController.h

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 
#import <AVFoundation/AVAudioPlayer.h> 
#import <AVFoundation/AVAudioRecorder.h> 

@interface ViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate> 

@property (weak, nonatomic) IBOutlet UIButton *RecordButton; 
@property (weak, nonatomic) IBOutlet UIButton *PlayButton; 

@property (weak, nonatomic) IBOutlet UIButton *PauseButton; 

- (IBAction)RecordButtonTapped:(id)sender; 

- (IBAction)PlayButtonTapped:(id)sender; 
- (IBAction)PauseButtonTapped:(id)sender; 


@end 

ViewController.M

#import "ViewController.h" 

@interface ViewController(){ 
    AVAudioRecorder *recorder; 
    AVAudioPlayer *player; 
} 


@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.PauseButton setEnabled:NO]; 
    [self.PlayButton setEnabled: NO]; 

    NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject], @"MyAudio.m4a", nil]; 

    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents]; 

    //setup Audio session 
    AVAudioSession *session = [AVAudioSession sharedInstance]; 
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 

    //define record settings 

    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:NULL]; 
    recorder.delegate = self; 
    recorder.meteringEnabled = YES; 
    [recorder prepareToRecord]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)RecordButtonTapped:(id)sender { 
    NSLog(@"RecordStart"); 
    if(player.playing){ 
     NSLog(@"Player stopped"); 
     [player stop]; 
    } 

    if(!recorder.recording){ 
     NSLog(@"Not recording yet"); 
     AVAudioSession *session = [AVAudioSession sharedInstance]; 
     [session setActive: YES error: nil]; 
     NSLog(@"Start session"); 
     //start recording 
     [recorder record]; 
     NSLog(@"record"); 
     [self.RecordButton setAttributedTitle:(NSAttributedString *) @"Pause" forState:UIControlStateNormal]; 
     NSLog(@"title crap"); 

    } else{ 
     [recorder pause]; 
     NSLog(@"she be paused"); 
     [self.RecordButton setAttributedTitle:(NSAttributedString *) @"Record" forState: UIControlStateNormal]; 
     NSLog(@"she title crap"); 
    } 
    [self.PauseButton setEnabled:YES]; 
    NSLog(@"pausebutton enabled"); 
    [self.PlayButton setEnabled:NO]; 
    NSLog(@"Play button not enabled"); 
} 


- (IBAction)PlayButtonTapped:(id)sender { 
    if(!recorder.recording){ 
     player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil]; 
     [player setDelegate:self]; 
     [player play]; 
    } 
} 

- (IBAction)PauseButtonTapped:(id)sender { 
    [recorder stop]; 
    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    [audioSession setActive:NO error: nil]; 
} 

-(void) audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{ 
    [self.RecordButton setAttributedTitle:(NSAttributedString *) @"Record" forState:UIControlStateNormal]; 
    [self.PauseButton setEnabled:NO]; 
    [self.PlayButton setEnabled:YES]; 
} 

-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Done" message:@"Finished Playing the recording!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
    [alert show]; 
} 
@end 

控制台输出

2014-11-14 12:12:14.495 AudioRecorder[1100:32446] RecordStart 
2014-11-14 12:12:14.497 AudioRecorder[1100:32446] Not recording yet 
2014-11-14 12:12:14.497 AudioRecorder[1100:32446] Start session 
2014-11-14 12:12:14.498 AudioRecorder[1100:32446] record 
2014-11-14 12:12:14.498 AudioRecorder[1100:32446] title crap 
2014-11-14 12:12:14.498 AudioRecorder[1100:32446] pausebutton enabled 
2014-11-14 12:12:14.499 AudioRecorder[1100:32446] Play button not enabled 
2014-11-14 12:12:14.499 AudioRecorder[1100:32446] -[__NSCFConstantString _ui_synthesizeAttributedSubstringFromRange:usingDefaultAttributes:]: unrecognized selector sent to instance 0x101bee288 
2014-11-14 12:12:14.505 AudioRecorder[1100:32446] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString _ui_synthesizeAttributedSubstringFromRange:usingDefaultAttributes:]: unrecognized selector sent to instance 0x101bee288' 
*** First throw call stack: 
(
    0 CoreFoundation      0x0000000102b81f35 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x0000000102471bb7 objc_exception_throw + 45 
    2 CoreFoundation      0x0000000102b8904d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
    3 CoreFoundation      0x0000000102ae127c ___forwarding___ + 988 
    4 CoreFoundation      0x0000000102ae0e18 _CF_forwarding_prep_0 + 120 
    5 UIKit        0x0000000103222293 -[UIButton _intrinsicSizeWithinSize:] + 685 
    6 UIKit        0x00000001035ed9a1 -[UIView(UIConstraintBasedLayout) intrinsicContentSize] + 37 
    7 UIKit        0x00000001035edea4 -[UIView(UIConstraintBasedLayout) _generateContentSizeConstraints] + 33 
    8 UIKit        0x00000001035edc64 -[UIView(UIConstraintBasedLayout) _updateContentSizeConstraints] + 422 
    9 UIKit        0x00000001035f50d6 -[UIView(AdditionalLayoutSupport) updateConstraints] + 163 
    10 UIKit        0x0000000103221e20 -[UIButton updateConstraints] + 2925 
    11 UIKit        0x00000001035f46fa -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 248 
    12 UIKit        0x00000001035f48f2 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 124 
    13 CoreFoundation      0x0000000102a8a194 CFArrayApplyFunction + 68 
    14 UIKit        0x00000001035f469b -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 153 
    15 Foundation       0x0000000102017d6e -[NSISEngine withBehaviors:performModifications:] + 155 
    16 UIKit        0x00000001035f48f2 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 124 
    17 UIKit        0x00000001035f4dbe __60-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]_block_invoke + 96 
    18 Foundation       0x0000000102017d6e -[NSISEngine withBehaviors:performModifications:] + 155 
    19 UIKit        0x00000001035f4a86 -[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded] + 231 
    20 UIKit        0x00000001035f519e -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded] + 159 
    21 UIKit        0x0000000102fdbb2d -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 114 
    22 UIKit        0x0000000102fe7973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521 
    23 QuartzCore       0x00000001053bcde8 -[CALayer layoutSublayers] + 150 
    24 QuartzCore       0x00000001053b1a0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380 
    25 QuartzCore       0x00000001053b187e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
    26 QuartzCore       0x000000010531f63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242 
    27 QuartzCore       0x000000010532074a _ZN2CA11Transaction6commitEv + 390 
    28 QuartzCore       0x0000000105320db5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89 
    29 CoreFoundation      0x0000000102ab6dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
    30 CoreFoundation      0x0000000102ab6d20 __CFRunLoopDoObservers + 368 
    31 CoreFoundation      0x0000000102aacb53 __CFRunLoopRun + 1123 
    32 CoreFoundation      0x0000000102aac486 CFRunLoopRunSpecific + 470 
    33 GraphicsServices     0x0000000105a5c9f0 GSEventRunModal + 161 
    34 UIKit        0x0000000102f6e420 UIApplicationMain + 1282 
    35 AudioRecorder      0x0000000101beafd3 main + 115 
    36 libdyld.dylib      0x000000010673f145 start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
+0

试着改变你所有的'setAttributedTitle的:(NSAttributedString *)''到的setTitle:'。 – AMI289 2014-11-14 12:35:47

回答

1

虽然我已经回答了上面的评论,但我也将修改过的代码放在这里,所以如果您愿意的话,您可以复制它。
我刚刚更换了所有的setAttributedTitle:(NSAttributedString *)setTitle:

- (IBAction)RecordButtonTapped:(id)sender { 
    NSLog(@"RecordStart"); 
    if(player.playing){ 
     NSLog(@"Player stopped"); 
     [player stop]; 
    } 

    if(!recorder.recording){ 
     NSLog(@"Not recording yet"); 
     AVAudioSession *session = [AVAudioSession sharedInstance]; 
     [session setActive: YES error: nil]; 
     NSLog(@"Start session"); 
     //start recording 
     [recorder record]; 
     NSLog(@"record"); 
     [self.RecordButton setTitle:@"Pause" forState:UIControlStateNormal]; 
     NSLog(@"title crap"); 

    } else{ 
     [recorder pause]; 
     NSLog(@"she be paused"); 
     [self.RecordButton setTitle:@"Record" forState: UIControlStateNormal]; 
     NSLog(@"she title crap"); 
    } 
    [self.PauseButton setEnabled:YES]; 
    NSLog(@"pausebutton enabled"); 
    [self.PlayButton setEnabled:NO]; 
    NSLog(@"Play button not enabled"); 
} 


-(void) audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{ 
    [self.RecordButton setTitle:@"Record" forState:UIControlStateNormal]; 
    [self.PauseButton setEnabled:NO]; 
    [self.PlayButton setEnabled:YES]; 
} 
+0

啊!最初我使用了'setTitle',但遇到了无法找到'setTitle'的错误。明显是我的错误,谢谢:) – 2014-11-14 13:01:51