gpt4 book ai didi

ios - Objective-c 没有已知的选择器类方法 'playAtTime'

转载 作者:行者123 更新时间:2023-11-28 21:39:36 24 4
gpt4 key购买 nike

我正在尝试使用 AVAudioPlayerplayAtTime 将闹钟编程为在用户设置的时间响起,当我使用此代码时 [AVAudioPlayer playAtTime:日期时间字符串];
尝试让它在用户设置的时间播放,我收到此错误 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com