【发布时间】:2011-05-24 21:41:34
【问题描述】:
我写了以下代码 但是音频部分仍然没有响应
#import "AccelerometerViewController.h"
@implementation AccelerometerViewController
@synthesize imageView;
@synthesize player;
- (void)viewDidLoad {
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 1.0f/60.0f;
carRadius = imageView.frame.size.width / 2;
delta = CGPointMake(12.0,4.0);
translation = CGPointMake(0.0,0.0);
[super viewDidLoad];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration {
if (acceleration.x>0) delta.x = 2; else delta.x = -2;
if (acceleration.y>0) delta.y = -2; else delta.y = 2;
[UIView beginAnimations:@"translate" context:nil];
imageView.transform =
CGAffineTransformMakeTranslation(translation.x, translation.y);
translation.x = translation.x + delta.x;
translation.y = translation.y + delta.y;
[UIView commitAnimations];
if (imageView.center.x + translation.x > 320 - carRadius ||
imageView.center.x + translation.x < carRadius) {
translation.x -= delta.x;
}
if (imageView.center.y + translation.y > 460 - carRadius ||
imageView.center.y + translation.y < carRadius) {
translation.y -= delta.y;
}
//playing Audio file to show warning if my car moved on to another lane
if (imageView.center.x + translation.x > 160)
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"tone" ofType:@"m4r"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p =[[AVAudioPlayer alloc]
initWithContentsOfURL:file error:nil];
[file release];
self.player = p;
[p release];
[player prepareToPlay];
[player setDelegate:self];
[self.player play];
}
}
谁能帮帮我
【问题讨论】:
-
if 语句 if (imageView.center.x + translation.x > 160) 是否被正确调用?添加 NSLog(@"播放警告音频");或要测试的东西。您遇到什么错误(如果有)?另外,你为什么要测试中心+翻译?据我了解,您将要移动汽车,那么为什么不在移动汽车后测试中心位置呢?
-
是的,当我使用 NSLog 消息进行测试时,它可以正常工作,但是当我尝试播放音频时,它会被击中。
-
通过“struck”我假设您的意思是输入“stuck”。您的意思是音频在播放过程中停止,它根本不播放任何东西,您的应用程序崩溃,或者您收到错误?请提供任何错误消息。
-
哦,是的,我的意思是卡住了。我的应用程序没有崩溃。但是当我尝试使用调试器时,我意识到代码从这一行停止工作“NSURL *file = [[NSURL alloc] initFileURLWithPath:path];”我不知道发生了什么事..
标签: iphone iphone-sdk-3.0 ios-simulator