I've hit a deadend trying to figure out how to get sounds to only play on the current view. Both work fine when they are run through the first time, but after that, only the second view's audio plays. I can't figure out how to get the sound to unload.
I'm using AVAudioPlayer on the second view, and I release the player in dealloc. Where's the right spot to release it?
I was also just thinking too - if anyone has a priority or order path in which the main functions of .m files are executed that might help. here's the code I am using.....
Code:
#import "SecondViewController.h"
#import <AudioToolbox/AudioToolbox.h>
@implementation SecondViewController
@synthesize _player;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = kUpdateInterval;
// Make the array to store our AVAudioPlayer objects
_soundFiles = [[NSMutableArray alloc] initWithCapacity:3];
// Load the array with the sample file
NSURL *sound1URL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"yellowbu" ofType:@"wav"]];
self._player = [[AVAudioPlayer alloc] initWithContentsOfURL:sound1URL error:nil];
}
- (void)startPlayback{
if ([self._player play]){
self._player.delegate = self;
}
else
NSLog(@"Could not play %@\n", self._player.url);
}
- (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];
[_player release];
}
#pragma mark -
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
if (sqrt(acceleration.x * acceleration.x) > kAccelerationThreshold) {
// play the sound file
[self startPlayback];
}
}
#pragma mark AVAudioPlayer delegate methods
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
if (flag == NO)
NSLog(@"Playback finished unsuccessfully");
[player setCurrentTime:0.];
}
@end
Thanks in advance.....