2012-06-29 61 views
0

所以我試圖用視圖的連接來啓動一個伴有搖動的聲音文件。我的代碼拋出錯誤沒有可見@interface爲'AVAudioPlayer'聲明選擇器'播放'。使用AVAudioPlayer嘗試使用運動手勢不起作用

這是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 
+0

任何建議??? –

回答

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]; 
    } 
} 
+0

這回答了我的問題,它工作!謝謝我在WYOMING戰鬥中消失了,終於回來試試了! –

相關問題