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 03-30-2009, 07:36 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
starwarsdevwookie59 is on a distinguished road
Default Playing sounds

I'm trying to create a program that simply plays and stops sound. I've been using the AVFoundation framework, but it hasn't seemed very efficient. Can anybody give me some code to play and stop sound. please be specific as to what files to place code in. thank you.

this is the code we hav been using:

NSString *path = [[NSBundle mainBundle] pathForResource:@"uranus" ofType:@"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
theAudio.delegate = self;
[theAudio play];

any help would be much appreciated.
starwarsdevwookie59 is offline   Reply With Quote
Old 03-30-2009, 07:48 PM   #2 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: United States
Posts: 132
Knertified is on a distinguished road
Default

I wrote this code for someone else on here. He had good luck with it. This will randomly play 10 wave files named sound1.wav through sound10.wav. Maybe it will help a bit...

Code:
int randomNumber = arc4random() % 10;
			
NSString *tmpFileName = [[NSString alloc] initWithFormat:@"sound%d", randomNumber];
NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileName ofType:@"wav"];
						
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:fileName], &soundID);
AudioServicesPlaySystemSound (soundID);
						
[tmpFileName release];
Knertified is offline   Reply With Quote
Old 03-30-2009, 09:27 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
starwarsdevwookie59 is on a distinguished road
Default

thanks for your help. for anyone reading this, the code i ended up using to play "sound1.wav" was:

NSString *tmpFileName = [[NSString alloc] initWithFormat:@"sound1"];
NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileName ofType:@"wav"];

SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:fileName], &soundID);
AudioServicesPlaySystemSound (soundID);

[tmpFileName release];

Last edited by starwarsdevwookie59; 04-03-2009 at 02:28 PM.
starwarsdevwookie59 is offline   Reply With Quote
Old 04-21-2009, 09:02 PM   #4 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 2
Bryan is on a distinguished road
Default Playing sounds from a thread...

Quote:
Originally Posted by starwarsdevwookie59 View Post
thanks for your help. for anyone reading this, the code i ended up using to play "sound1.wav" was:
NSString *tmpFileName = [[NSString alloc] initWithFormat:@"sound1"];
NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileName ofType:@"wav"];

SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:fileName], &soundID);
AudioServicesPlaySystemSound (soundID);

[tmpFileName release];
I am using this pattern to play audio. It works fine when initiated from a UI action such as pressing a button. I am implementing an animation sequence with sound in a thread which is dispatched as follows:

Code:
	[NSThread detachNewThreadSelector:@selector(displayAnimation:) toTarget:self withObject:nil];
Now, this all works fine. In my selector I have to call the following to get render to my view:

Code:
	for ( int i = 0; i < c; i++ ) {
		KeyFrame *frame = [frameArray objectAtIndex:i];
		
		int waitTime = [frame duration];
		UIImage *image = [frame image];
		
		[self performSelectorOnMainThread:@selector(drawFrame:) withObject:image waitUntilDone:YES];
		usleep(waitTime * 1000.0);
	}
I added the same pattern to play the sound thinking that it needs to be initiated from the main thread but nothing.

Code:
	if ( soundPath != nil ) {
		[self performSelectorOnMainThread:@selector(playSound:) withObject:soundPath waitUntilDone:YES];
	}

...

-(void) playSound:(id)path
{
	NSLog( @"Playing sound from path: %@\n", path );
	SystemSoundID soundID;
	AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path], &soundID);
	AudioServicesPlaySystemSound(soundID);	
}
I tried adding the code:

Code:
	NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"you_are_nice" ofType:@"mp3"];
	SystemSoundID soundID;
	AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:soundPath], &soundID);
	AudioServicesPlaySystemSound(soundID);
To the beginning of the viewDidLoad and still nothing. When I look at the package in ../Library/Application Support/iPhone Simulator/... I see the mp3 file so I am at a loss.

Any help debugging this or suggestions what to look at would be very grateful.

Thanks!
Bryan
Bryan is offline   Reply With Quote
Old 04-22-2009, 07:47 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 26
kgodard is on a distinguished road
Default

It might be because you're using the system sound, it's limited what kind of files you can play through that. I've got a way to play any sound file the iPhone can play on our site. Try that one out.
__________________
Shablabla
kgodard is offline   Reply With Quote
Old 04-22-2009, 10:46 AM   #6 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 2
Bryan is on a distinguished road
Default

You are correct - turns out that the AudioServices does not play compressed audio like MP3.
Bryan is offline   Reply With Quote
Old 08-26-2009, 02:16 AM   #7 (permalink)
shiva
 
shiva.0537's Avatar
 
Join Date: Jun 2009
Location: Hyderabad
Age: 26
Posts: 54
shiva.0537 is on a distinguished road
Send a message via Skype™ to shiva.0537
Default

Quote:
Originally Posted by Knertified View Post
I wrote this code for someone else on here. He had good luck with it. This will randomly play 10 wave files named sound1.wav through sound10.wav. Maybe it will help a bit...

Code:
int randomNumber = arc4random() % 10;
			
NSString *tmpFileName = [[NSString alloc] initWithFormat:@"sound%d", randomNumber];
NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileName ofType:@"wav"];
						
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:fileName], &soundID);
AudioServicesPlaySystemSound (soundID);
						
[tmpFileName release];
Hi,

Will this code works with m4a format?
On simulator as well as on device?

Thanks
shiva
shiva.0537 is offline   Reply With Quote
Reply

Bookmarks

Tags
audio, play, simple, sound, stop

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: 341
5 members and 336 guests
blueorb, guusleijsten, Kryckter, LEARN2MAKE, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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