crazy_tiger_corp crazy_tiger_corp - 1 year ago 147
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

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;

@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: ];
NSLog( @"Set button tapped : %@", dateTimeString );

[AVAudioPlayer playAtTime:dateTimeString];

[self scheduleLocalNotificationWithDate:];


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


And the viewcontroller.h

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

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

-(IBAction) alarmSetButtonTapped:(id)sender;


How do I fix this error and use


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download