crazy_tiger_corp crazy_tiger_corp - 3 months ago 36
iOS Question

Objective-c no known class method for selector 'playAtTime'

I'm trying to program an alarm to go off at a user set time using

AVAudioPlayer
and
playAtTime
and when I use this code
[AVAudioPlayer playAtTime:dateTimeString];
to try and get it to play at the user set time, I get this error
no known class method for selector 'playAtTime'
. The rest of my code is as follows

#import "ViewController.h"

@interface ViewController()
{
AVAudioPlayer *_myPlayer;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSString *path = [NSString stringWithFormat:@"%@/drum01.mp3", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
_alarmPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];

}

-(IBAction) alarmSetButtonTapped:(id)sender {

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date ];
NSLog( @"Set button tapped : %@", dateTimeString );

[AVAudioPlayer playAtTime:dateTimeString];

[self scheduleLocalNotificationWithDate: dateTimePicker.date];

}

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

@end


And the viewcontroller.h

#import <UIKit/UIKit.h>
#import <AvFoundation/AVFoundation.h>

@interface ViewController : UIViewController <AVAudioPlayerDelegate>
{
IBOutlet UIDatePicker *dateTimePicker;
}


-(IBAction) alarmSetButtonTapped:(id)sender;

@end


How do I fix this error and use
playAtTime
?

Thanks

Answer

The -[AVAudioPlayer playAtTime:] method is an instance method, not a class method, so it must be called on an instance (e.g, _alarmPlayer). Additionally, its argument is a NSTimeInterval, not a string.

In any case, this method does not do what you want here. It is used to start the player from a point other than its beginning; the argument indicates how far into the sample it should start. It does not schedule the audio sample to be played at a time in the future.