Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-09-2010, 10:01 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 16
retsoced is on a distinguished road
Default View specific audio overlapping views

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.....
retsoced is offline   Reply With Quote
Old 01-10-2010, 07:54 AM   #2 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 16
retsoced is on a distinguished road
Default

Okay, well I fixed my own problem after looking through the docs at what fires when and how often.

Using viewWillAppear correctly loads the sounds I need on each view when it gets accessed.....
retsoced is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 337
15 members and 322 guests
akacaj, alexP, ClerurcifeDer, Domele, Duncan C, givensur, GraffitiCircus, JmayLive, michelle, NetGuru, NSString, Paul Slocum, Sloshmonster, soohyun, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:52 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0