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-27-2012, 08:02 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 3
jjgoldman is on a distinguished road
Default Help! Audio stopping problem.

Hi guys, new to these forums and ios development.

I am trying to create a simple app which has a button which when touched will play a random sound from a series. When I click the button, it does play the sound, but when touched again it will play the second sound over the first. How can I make the previous song stop when the button is touched again?

Thanks in advance- below is the code.

Code:
- (IBAction)randomsound {
	NSString *title = nil;
	NSString *path = nil;
	AVAudioPlayer *audio = nil;
  
	
	int Number = arc4random() % 2;
	switch (Number) {
		case 0:
			path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
			audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];		
            [audio stop];
            [audio play]; 
			break;
			
		case 1:
			path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"wav"];
			audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];	
            [audio stop];
            [audio play]; 
			break;
			
    
			break;
		default:
			break;
            
                            
	}
	
	
}
jjgoldman is offline   Reply With Quote
Old 01-27-2012, 08:54 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 25
veterinarian2008 is on a distinguished road
Default

Quote:
Originally Posted by jjgoldman View Post
Hi guys, new to these forums and ios development.

I am trying to create a simple app which has a button which when touched will play a random sound from a series. When I click the button, it does play the sound, but when touched again it will play the second sound over the first. How can I make the previous song stop when the button is touched again?

Thanks in advance- below is the code.

Code:
- (IBAction)randomsound {
	NSString *title = nil;
	NSString *path = nil;
	AVAudioPlayer *audio = nil;
  
	
	int Number = arc4random() % 2;
	switch (Number) {
		case 0:
			path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
			audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];		
            [audio stop];
            [audio play]; 
			break;
			
		case 1:
			path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"wav"];
			audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];	
            [audio stop];
            [audio play]; 
			break;
			
    
			break;
		default:
			break;
            
                            
	}
	
	
}

Look at the logic of your code. You have stop and play in the same actions which it won't do anything since you call it stop and then play. Of curse the audio will keep playing.
veterinarian2008 is offline   Reply With Quote
Old 01-27-2012, 08:57 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 25
veterinarian2008 is on a distinguished road
Default

Quote:
Originally Posted by veterinarian2008 View Post
Look at the logic of your code. You have stop and play in the same actions which it won't do anything since you call it stop and then play. Of curse the audio will keep playing.
If I was you, I would declare 2 different AVAudioPlayers. One for the first and the other one for the second.
veterinarian2008 is offline   Reply With Quote
Old 01-27-2012, 09:01 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 3
jjgoldman is on a distinguished road
Default

ah ok thanks. I will give it a try and post back with my results.
jjgoldman is offline   Reply With Quote
Old 01-27-2012, 09:37 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 3
jjgoldman is on a distinguished road
Default

Quote:
If I was you, I would declare 2 different AVAudioPlayers. One for the first and the other one for the second.
As I said, Im very new to this whole thing. I'm not entirly sure what you meant but gave it ago :s, what I tried didn't work, could you (or someone else) help me correct this.

Thanks again!

Code:
- (IBAction)randomsound {
	NSString *title = nil;
	NSString *path = nil;
	AVAudioPlayer *audio = nil;
    AVAudioPlayer *stopaudio;
  
	
	int Number = arc4random() % 2;
	switch (Number) {
		case 0:
			path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
			audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
            stopaudio =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
            [stopaudio stop];
            [audio play]; 
			break;
			
		case 1:
			path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"wav"];
			audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];	
            stopaudio =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
            [stopaudio stop];
            [audio play]; 
			break;
			
    
			break;
		default:
			break;
            
                            
	}
	
	
}
jjgoldman is offline   Reply With Quote
Old 01-27-2012, 11:00 AM   #6 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 25
veterinarian2008 is on a distinguished road
Default

Quote:
Originally Posted by jjgoldman View Post
As I said, Im very new to this whole thing. I'm not entirly sure what you meant but gave it ago :s, what I tried didn't work, could you (or someone else) help me correct this.

Thanks again!

Code:
- (IBAction)randomsound {
	NSString *title = nil;
	NSString *path = nil;
	AVAudioPlayer *audio = nil;
    AVAudioPlayer *stopaudio;
  
	
	int Number = arc4random() % 2;
	switch (Number) {
		case 0:
			path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
			audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
            stopaudio =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
            [stopaudio stop];
            [audio play]; 
			break;
			
		case 1:
			path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"wav"];
			audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];	
            stopaudio =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
            [stopaudio stop];
            [audio play]; 
			break;
			
    
			break;
		default:
			break;
            
                            
	}
	
	
}

This thread has been answered before:


http://www.iphonedevsdk.com/forum/ip...tion-help.html

Just read over. Your not too far. Also, those switch statements are sometimes a bit tricky. Ill rather use and "if" statement.

Good luck
veterinarian2008 is offline   Reply With Quote
Old 01-27-2012, 04:35 PM   #7 (permalink)
[[Brain alloc]init];
 
fhsjaagshs's Avatar
 
Join Date: Aug 2011
Location: New Jersey
Age: 15
Posts: 92
fhsjaagshs is on a distinguished road
Default

You need AVAudioSession:

Code:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setDelegate:self];
I'd also like to mention that you must release the audio in the dealloc method because:
1. You don't want memory leaks
2. Releasing the AVAudioPlayer causes the audio to stop (Audio is loaded to the memory (RAM), releasing removes something from the memory)

Last edited by fhsjaagshs; 01-27-2012 at 04:37 PM.
fhsjaagshs 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: 411
10 members and 401 guests
Atatator, condor304, FrankWeller, imac74, MAMN84, mraalex, n00b, PowerGoofy, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

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