AVAudioPlayerdelegate

0 xcode objective-c interface-builder

我收到此代码的错误 -

警告:类'BeatMaker'未实现'AVAudioPlayerDelegate'协议

-(IBAction)playBeat3 {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"beat3" ofType:@"mp3"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self;
    [theAudio play];                         
}
Run Code Online (Sandbox Code Playgroud)

谁能帮我这个?

Sea*_*ell 7

您告诉AVAudioPlayer实例,您的BeatMaker类使用以下行实现AVAudioPlayerDelegate协议:

theAudio.delegate = self;
Run Code Online (Sandbox Code Playgroud)

但显然你的BeatMaker类没有告诉编译器它实际上是一个AVAudioPlayerDelegate.你会在头文件中这样做:

@interface BeatMaker : NSObject <AVAudioPlayerDelegate> { ... }
Run Code Online (Sandbox Code Playgroud)

然后,您必须确保已在BeatMaker类中实现了AVAudioPlayerDelegate协议的所有必需功能.

在这种情况下,协议没有必需的功能,因此如果您只是复制代码并且实际上并不想从音频接收消息,则可以删除将self分配给音频的delegate属性的行.