Objective-c 没有已知的 class 选择器方法 'playAtTime'
Objective-c no known class method for selector 'playAtTime'
我正在尝试使用 AVAudioPlayer
和 playAtTime
将闹钟编程为在用户设定的时间响起,当我使用此代码 [AVAudioPlayer playAtTime:dateTimeString];
尝试将其设置为在用户设置的时间播放,我收到此错误 no known class method for selector 'playAtTime'
。我的其余代码如下
#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
和 viewcontroller.h
#import <UIKit/UIKit.h>
#import <AvFoundation/AVFoundation.h>
@interface ViewController : UIViewController <AVAudioPlayerDelegate>
{
IBOutlet UIDatePicker *dateTimePicker;
}
-(IBAction) alarmSetButtonTapped:(id)sender;
@end
如何修复此错误并使用 playAtTime
?
谢谢
-[AVAudioPlayer playAtTime:]
方法是一个实例方法,不是class方法,所以它必须在一个实例上调用(例如,_alarmPlayer
)。此外,它的参数是 NSTimeInterval
,而不是字符串。
无论如何,这里的这个方法不会做你想做的。它用于从开始点以外的点开始播放器;该参数表示它应该从样本中开始多远。它不会安排在将来的某个时间播放音频样本。
我正在尝试使用 AVAudioPlayer
和 playAtTime
将闹钟编程为在用户设定的时间响起,当我使用此代码 [AVAudioPlayer playAtTime:dateTimeString];
尝试将其设置为在用户设置的时间播放,我收到此错误 no known class method for selector 'playAtTime'
。我的其余代码如下
#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
和 viewcontroller.h
#import <UIKit/UIKit.h>
#import <AvFoundation/AVFoundation.h>
@interface ViewController : UIViewController <AVAudioPlayerDelegate>
{
IBOutlet UIDatePicker *dateTimePicker;
}
-(IBAction) alarmSetButtonTapped:(id)sender;
@end
如何修复此错误并使用 playAtTime
?
谢谢
-[AVAudioPlayer playAtTime:]
方法是一个实例方法,不是class方法,所以它必须在一个实例上调用(例如,_alarmPlayer
)。此外,它的参数是 NSTimeInterval
,而不是字符串。
无论如何,这里的这个方法不会做你想做的。它用于从开始点以外的点开始播放器;该参数表示它应该从样本中开始多远。它不会安排在将来的某个时间播放音频样本。