我正在尝试使用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:]
方法是实例方法,而不是类方法,因此必须在实例(例如_alarmPlayer
)上调用它。此外,其参数是NSTimeInterval
,而不是字符串。
无论如何,此方法都无法满足您的要求。它用于从播放器以外的其他位置启动播放器;参数表示应该从样本开始多远。它不会计划在将来的某个时间播放音频样本。
关于ios - Objective-C没有已知的选择器'playAtTime'的类方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32676517/