2011-04-19 46 views
1

你好 我试图拥有它,所以一旦用户摇动设备。我想要一个声音播放。然而,一旦我动摇它崩溃的设备,这是我所使用的加速度计 - 震动后崩溃 - 可可触摸

- (BOOL)canBecomeFirstResponder 
{ 
    return YES; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    [self becomeFirstResponder]; 
} 

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{ 
    if(motion == UIEventSubtypeMotionShake) 
    { 
     NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"]; 
     if (theAudio) [theAudio release]; 
     NSError *error = nil; 
     theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; 
     if (error) 
      NSLog(@"%@",[error localizedDescription]); 
     theAudio.delegate = self; 
     [theAudio play];  
    } 
} 

的代码,这是由于崩溃报告的原因崩溃报告

2011-04-19 19:25:44.337 iApp[314:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00fc3be9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x00db85c2 objc_exception_throw + 47 
    2 CoreFoundation      0x00f7c628 +[NSException raise:format:arguments:] + 136 
    3 CoreFoundation      0x00f7c59a +[NSException raise:format:] + 58 
    4 Foundation       0x00055b12 -[NSURL(NSURL) initFileURLWithPath:] + 90 
    5 Foundation       0x00055aa0 +[NSURL(NSURL) fileURLWithPath:] + 72 
    6 iApp        0x0000305d -[AppViewController motionEnded:withEvent:] + 256 
    7 UIKit        0x002dc07c -[UIWindow sendEvent:] + 350 
    8 UIKit        0x002bf37a -[UIApplication sendEvent:] + 447 
    9 UIKit        0x002c311b _UIApplicationHandleEvent + 1921 
    10 GraphicsServices     0x017daa36 PurpleEventCallback + 1550 
    11 CoreFoundation      0x00fa5064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 
    12 CoreFoundation      0x00f056f7 __CFRunLoopDoSource1 + 215 
    13 CoreFoundation      0x00f02983 __CFRunLoopRun + 979 
    14 CoreFoundation      0x00f02240 CFRunLoopRunSpecific + 208 
    15 CoreFoundation      0x00f02161 CFRunLoopRunInMode + 97 
    16 GraphicsServices     0x017d9268 GSEventRunModal + 217 
    17 GraphicsServices     0x017d932d GSEventRun + 115 
    18 UIKit        0x002c842e UIApplicationMain + 1160 
    19 iApp        0x0000295c main + 102 
    20 iApp        0x000028ed start + 53 
) 
terminate called after throwing an instance of 'NSException' 
+1

是你的文件sound.wav? – 2011-04-19 18:50:56

回答

2

reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' 

以及应用程序代码中最后执行的行:

6 iApp    0x0000305d -[AppViewController motionEnded:withEvent:] + 256 

我会认为你sound.wav文件丢失:

NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"]; 

我肯定建议你化妆:

  1. 的sound.wav文件是在您的项目
  2. 的情况下,敏感拼写“ sound.wav“

希望这会有所帮助。

+0

哦,是的,我的坏,是sound.wma:S感谢人们! – LAA 2011-04-19 18:56:44

+0

在那里我自己。很高兴帮助。 – dredful 2011-04-19 18:59:26

+1

@LAA,你应该投票并接受答案,因为它是正确的。 – 2011-04-19 19:09:57