Note that there are some explanatory texts on larger screens.

plurals
  1. POAVAudioRecorder and AVAudioPlayer
    text
    copied!<p>It's day three and I still can't get playback. I've been following the few tutorials on AVAudioPlayer/AVAudioRecorder. Using NSFileManager it looks like a file is created, but no dice still on that playback.</p> <p>RecorderViewController.m</p> <pre><code>// RecorderViewController.m // AudioTest // #import "RecorderViewController.h" #import &lt;Foundation/Foundation.h&gt; @interface RecorderViewController () @end @implementation RecorderViewController @synthesize userIsRecording, filePath, activityView, recordButton, playButton, recorder; /* - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } */ + (CGRect)makeCGRectWithCenter:(CGPoint)center width:(float)width height:(float)height { return CGRectMake(center.x-width/2, center.y-height/2, width, height); } #pragma mark - Preparation - (void)loadView { // RECORD BUTTON self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; self.recordButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; self.recordButton.frame = [[self class] makeCGRectWithCenter:CGPointMake(self.view.frame.size.width/2, 100) width:150 height:50]; [self.recordButton setTitle:@"Record" forState:UIControlStateNormal]; [self.recordButton addTarget:self action:@selector(recordPressed) forControlEvents:UIControlEventTouchUpInside]; // PLAY BUTTON self.playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; self.playButton.frame = [[self class] makeCGRectWithCenter:CGPointMake(self.view.frame.size.width/2, 200) width:150 height:50]; [self.playButton setTitle:@"Play" forState:UIControlStateNormal]; [self.playButton addTarget:self action:@selector(playPressed) forControlEvents:UIControlEventTouchUpInside]; // RETURN BUTTON UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; returnButton.frame = [[self class] makeCGRectWithCenter:CGPointMake(self.view.frame.size.width/2, 300) width:150 height:50]; [returnButton setTitle:@"Return" forState:UIControlStateNormal]; [returnButton addTarget:self action:@selector(dismissPressed:) forControlEvents:UIControlEventTouchUpInside]; // ACTIVITY self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; self.activityView.frame = [[self class] makeCGRectWithCenter:CGPointMake(self.view.frame.size.width/2, 50) width:100 height:100]; [self.view addSubview:self.recordButton]; [self.view addSubview:self.playButton]; [self.view addSubview:returnButton]; [self.view addSubview:self.activityView]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"View did load"); filePath = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"temp2.caf"]]; // Setup AudioSession AVAudioSession *avSession = [AVAudioSession sharedInstance]; [avSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL]; [avSession setActive:YES error: NULL]; self.playButton.hidden = YES; } #pragma mark - Button Actions - (void)dismissPressed:(id)sender { if ([sender isKindOfClass:[UIButton class]]) { NSLog(@"Button class dismissed self"); } else { NSLog(@"Sender is:%@", [sender class]); } [self dismissModalViewControllerAnimated:YES]; } - (void)stopPressed { NSLog(@"Stop Pressed"); [self.recordButton setTitle:@"Record" forState:UIControlStateNormal]; self.userIsRecording = NO; self.playButton.hidden = NO; self.playButton.enabled = YES; [self.activityView stopAnimating]; // } - (void)recordPressed { if (self.userIsRecording) { [self stopPressed]; } else { self.userIsRecording = YES; self.playButton.enabled = NO; self.playButton.hidden = YES; [self.recordButton setTitle:@"Stop" forState:UIControlStateNormal]; [self.activityView startAnimating]; NSDictionary *recorderSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey, [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey, [NSNumber numberWithInt:16], AVEncoderBitRateKey, [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, [NSNumber numberWithFloat:8000.0], AVSampleRateKey, [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey, nil]; // Clean temp file NSFileManager * fm = [NSFileManager defaultManager]; [fm removeItemAtPath:[self.filePath path] error:NULL]; // Record NSError *error = [NSError alloc]; self.recorder = [[AVAudioRecorder alloc] initWithURL:self.filePath settings:recorderSettings error:&amp;error]; [recorder setDelegate:self]; [recorder prepareToRecord]; if (![recorder record]) { NSLog(@"Recorder FAIL %@", error ); } else { NSLog(@"Recording at %@", [self.filePath absoluteString]); } } } - (void)playPressed { NSFileManager * fm = [NSFileManager defaultManager]; if ([fm fileExistsAtPath:[self.filePath path]]) { NSLog(@"File exists at:%@", [self.filePath path]); NSDictionary *attr = [fm attributesOfItemAtPath:[self.filePath path] error:NULL]; NSLog(@"File attrs:%@", [attr description]); } else { NSLog(@"ERROR: No file exists at:%@", [self.filePath path]); } NSError *error = [[NSError alloc] init]; AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:[[NSData alloc] initWithContentsOfURL:self.filePath] error: &amp;error]; [player setDelegate:self]; if (error) { NSLog(@"Player initialization Error: %@", error); } if (!player) { NSLog(@"Player is null!"); } [player prepareToPlay]; if (![player play]) { NSLog(@"Play Error: %@", error); } } #pragma mark - Lifecycle - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. NSLog(@"View did unload"); } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload