因此,我尝试使用与视图的连接来启动带有摇晃动作的声音文件。我的代码抛出错误 No visible @interface for 'AVAudioPlayer' 声明选择器 'playsound'。
这是 ViewController.m
#import "aniMateViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface aniMateViewController ()
@end
@implementation aniMateViewController
@synthesize playSound;
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake)
{ NSLog(@"Device started shaking!");
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/play.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer playSound];
}
}
h文件在这里
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface aniMateViewController : UIViewController
{
AVAudioPlayer *audioPlayer;
IBOutlet UIView *start;
}
@property (weak, nonatomic) IBOutlet UIView *playSound;
@end