我写了以下代码 但仍然音频部分没有响应
#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];
}
}
任何人都可以帮助我
答案 0 :(得分:2)
以下是要检查的一些事项:确保已将AVFoundation框架导入到项目中,并且头文件中包含#import <AVFoundation/AVAudioPlayer.h>
。
将所有[对象释放]方法移动到块的底部。它不应该有所作为,但值得一试。
您使用的是最新版本的iOS吗? 4.2.1是最新的。
确保您的音频文件名称为tone.m4r - 区分大小写。
在左侧边栏中,展开“目标”,然后展开应用名称,然后展开“复制捆绑资源”。文件tone.m4r在那里?如果没有,请将其拖入。
最后,为什么要创建AVAudioPlayer * p,然后将其设置为self.player(我假设您已在头文件中定义)?相反,如果你还没有,请在@interface声明AVAudioPlayer *player;
的头文件中。在@interface块之后但在@end之前添加@property (nonatomic, retain) AVAudioPlayer *player;
如果你还没有。
替换
AVAudioPlayer *p =[[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[file release];
self.player = p;
[p release];
与
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[file release];
编辑:
•您不需要[player setDelegate:self];
•确保您拥有self.player
,而不只是player
(不应该有所作为,但它更具描述性)。
•我创建了一个全新的空白项目来测试您的/我的代码。见下文。
AudioTesterViewController.h:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface AudioTesterViewController : UIViewController {
AVAudioPlayer *player;
}
@property (nonatomic, retain) AVAudioPlayer *player;
- (IBAction)playAudio;
@end
AudioTesterViewController.m:
#import "AudioTesterViewController.h"
@implementation AudioTesterViewController
@synthesize player;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)playAudio {
NSString *path = [[NSBundle mainBundle] pathForResource:@"tone" ofType:@"m4r"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
[path release];
self.player =[[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[file release];
[self.player prepareToPlay];
[self.player play];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
注意: - (IBAction)playAudio是由我在Interface Builder中创建的按钮调用的,当然,您可以将其设置为void函数或将代码复制到另一个函数中。 如果此代码不起作用,请尝试单击菜单栏Build-&gt; Clean,然后运行项目。
这是我的文件结构: