Nav Link On

iPhone Dev Forums > General Development > SDK Coding Help > Play single audio sound at a time.

Reply

 

LinkBack Thread Tools Display Modes
Old 10-18-2009, 02:51 AM   #1 (permalink)
Junior Member
Default Avatar
 
Join Date: Oct 2009
Posts: 1
Default Play single audio sound at a time.

I have a few instrumental audio tracks that I have set up as buttons on my View. How can I set it up to where if I am playing one track and then press another button then the first track should stop playing and the second should start. Here is the code I am using right now, not sure what to add to do to make it do so. Thanks in advance.

Code:

You must Login or Register to view and contribute code! This is done to increase participation in helping one another out, if you have been helped please pass on the favor.

 - (void)instrumentalone {
	//play the result
	AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"instrumental1" ofType:@"wav"]] error:nil];
	if (nil != player) {
		[player setDelegate:self];   
		[player play];
	} else {
		NSLog(@"Failed to allocate AVAudioPlayer object!");
	}
}


- (void)instrumentaltwo {
	//play the result
	AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"instrumental2" ofType:@"wav"]] error:nil];
	if (nil != player) {
		[player setDelegate:self];   
		[player play];
	} else {
		NSLog(@"Failed to allocate AVAudioPlayer object!");
	}
}


- (void)instrumentalthree {
	//play the result
	AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"instrumental3" ofType:@"wav"]] error:nil];
	if (nil != player) {
		[player setDelegate:self];   
		[player play];
	} else {
		NSLog(@"Failed to allocate AVAudioPlayer object!");
	}
}

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
	if (flag == NO) NSLog(@"Playback finished successfully!");
	[player setDelegate:nil];
	[player release];
}

Last edited by kdp8791; 10-18-2009 at 02:54 AM.
kdp8791 is offline   Reply With Quote
Old 10-18-2009, 09:59 PM   #2 (permalink)
Junior Member
Default Avatar
 
Join Date: Aug 2009
Posts: 17
Default

Keep track of your players and then use [player1 stop];
david_skelly is offline   Reply With Quote
Reply

iPhone Dev Forums > General Development > SDK Coding Help


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


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0 RC1