0

因此,我尝试使用与视图的连接来启动带有摇晃动作的声音文件。我的代码抛出错误 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
4

1 回答 1

0

在接口中使用这个添加 AVAudioPlayer 委托:

  -(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]]];
      audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
      audioPlayer.delegate = self;
       [url release];

      [audioPlayer prepareToPlay];
      [audioPlayer play];
   }
 }
于 2012-06-29T05:43:35.497 回答