开发者

how to play audio file depending on the position of UIimageview which is moving by accelerometer

I wrote the following code But still the Audio part is not responding

#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 + translat开发者_StackOverflow中文版ion.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];


 }
 } 

Can any one please help me


Here are a few things to check: Make sure you have imported the AVFoundation framework into your project and that you have #import <AVFoundation/AVAudioPlayer.h> in your header file.

Move all of your [object release] methods to the bottom of the block. It should't make a difference but it's worth a try.

Are you using the latest version of iOS? 4.2.1 is the newest.

Make sure that your audio file is named precisely tone.m4r - it is case sensitive.

In the left sidebar, expand "Targets", then the name of your app, then "Copy Bundle Resources". Is the file tone.m4r in there? If not, drag it in.

Finally, why are you creating the AVAudioPlayer *p and then setting it equal to self.player (which I assume you've defined in your header file)? Instead, in your header file in @interface declare AVAudioPlayer *player; if you haven't already. After the @interface block but before the @end add @property (nonatomic, retain) AVAudioPlayer *player; if you haven't already.

Replace

AVAudioPlayer *p =[[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil]; 
[file release];

self.player = p;
[p release];

with

self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[file release];

Edits:

•You don't need [player setDelegate:self]; •Make sure you have self.player, not just player (shouldn't make a difference but it's more descriptive). •I created a totally new, blank project to test your/my code. See below.

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

Notes: -(IBAction)playAudio is called by a button I made in Interface Builder, but you can, of course, just make it a void function or copy the code into another function instead. If this code doesn't work, try clicking in the menu bar Build->Clean and then running the project.

Here is my file structure:

how to play audio file depending on the position of UIimageview which is moving by accelerometer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜