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-24-2009, 07:01 AM   #1 (permalink)
Registered Member
 
manicaesar's Avatar
 
Join Date: Feb 2009
Location: Poznań, Poland
Age: 25
Posts: 103
manicaesar is on a distinguished road
Default AVAudioPlayer - playing multiple sounds with one instance

Hi!

I have a question about using AVAudioPlayer and I wasn't able to find an answer.

In my application I have a tableView. With every cell in table view will be associated a different sound. When user selects some cell, the sound should be played, and when he selects another cell, the previous sound should stop and the new one should be played. I want to have only one instance of AudioPlayer for playing all the sounds. Is it possible? If not, do you have any suggestion, how to achieve that?

I'm only interested in how to 're-set' the player to play the new sound.

Thanks for your help!

Last edited by manicaesar; 03-24-2009 at 07:06 AM.
manicaesar is offline   Reply With Quote
Old 03-26-2009, 06:07 AM   #2 (permalink)
Registered Member
 
manicaesar's Avatar
 
Join Date: Feb 2009
Location: Poznań, Poland
Age: 25
Posts: 103
manicaesar is on a distinguished road
Post Solved

Ok, I've already solved it by myself.

It's simple: you just have to release previous player and alloc and init it again. It does not work at first, because I do not retain NSData object which I used to init the player object. My code looks something like that:

Code:
- (void)playerPlay:(id)sender {
	int idx =[listOfResultsTableView indexPathForSelectedRow].row;
	[player stop];
	[player setCurrentTime:0.0];
	[player release];
	NSData *cdata = [Searcher getAudioDataForSearchResult:[searchResults objectAtIndex:idx]];
	if(cdata == nil) {
		NSLog(@"cdata nil");
		return;
	}
	player = [[AVAudioPlayer alloc] initWithData:cdata error: nil];
	[player setVolume:0.1];
	[player play];
}
If someone in future needs help, just contact me
manicaesar is offline   Reply With Quote
Old 05-15-2009, 10:00 PM   #3 (permalink)
New Member
 
Join Date: May 2009
Location: in the jungle
Posts: 3
zhydenina is on a distinguished road
Send a message via AIM to zhydenina
Arrow

Hi manicaesar,

I've been working on something similar. In my app with a tableview, I have about 10 table cells each to be associated with 10 different sounds (.mp3). When a user selects a different cell, a different sound will be played. The question is, how do I associate each table cell with each sound file? Do I store those sound files in an array at the same time I name each table cell? how could i specifically achieve this, Thanks for your help



Quote:
Originally Posted by manicaesar View Post
Hi!

I have a question about using AVAudioPlayer and I wasn't able to find an answer.

In my application I have a tableView. With every cell in table view will be associated a different sound. When user selects some cell, the sound should be played, and when he selects another cell, the previous sound should stop and the new one should be played. I want to have only one instance of AudioPlayer for playing all the sounds. Is it possible? If not, do you have any suggestion, how to achieve that?

I'm only interested in how to 're-set' the player to play the new sound.

Thanks for your help!
zhydenina is offline   Reply With Quote
Old 05-16-2009, 06:19 AM   #4 (permalink)
Registered Member
 
manicaesar's Avatar
 
Join Date: Feb 2009
Location: Poznań, Poland
Age: 25
Posts: 103
manicaesar is on a distinguished road
Default

Quote:
Originally Posted by zhydenina View Post
Hi manicaesar,

I've been working on something similar. In my app with a tableview, I have about 10 table cells each to be associated with 10 different sounds (.mp3). When a user selects a different cell, a different sound will be played. The question is, how do I associate each table cell with each sound file? Do I store those sound files in an array at the same time I name each table cell? how could i specifically achieve this, Thanks for your help
Hi

Well, in my app, i have an array named searchResults, which you can see in my code excerpt. This array consists of SearchResult object (my own class) which contains text for the cell to display and an id number. This id number is associated with an id in some table in my SQLite database. When a user selects some cell, i get SearchResult object associated with this cell and use the id field to get filename for audio file (these filename are stored in database). Then I use NSPath and NSUrl to get the NSData for the audio. (I could not post the exact code for it till tuesday).

In your problem I think the acceptable solution could be creating some class which will contain a name for a table cell and a sound filename (or path) for audio file associated. Then you just have to create an NSArray and fill it with these 10 objects. You use the name in tableCellForRowAtIndexPath method (I do not remember exaclty its name ). When user selects some cell, you use index for this cell to get the object from array and then you get filename for your audio from this object.

I hope I explained it enough clearly
manicaesar is offline   Reply With Quote
Old 05-17-2009, 06:58 PM   #5 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 87
pmvinuelas is on a distinguished road
Default

Quote:
Originally Posted by manicaesar View Post
Ok, I've already solved it by myself.

It's simple: you just have to release previous player and alloc and init it again. It does not work at first, because I do not retain NSData object which I used to init the player object. My code looks something like that:

Code:
- (void)playerPlay:(id)sender {
	int idx =[listOfResultsTableView indexPathForSelectedRow].row;
	[player stop];
	[player setCurrentTime:0.0];
	[player release];
	NSData *cdata = [Searcher getAudioDataForSearchResult:[searchResults objectAtIndex:idx]];
	if(cdata == nil) {
		NSLog(@"cdata nil");
		return;
	}
	player = [[AVAudioPlayer alloc] initWithData:cdata error: nil];
	[player setVolume:0.1];
	[player play];
}
If someone in future needs help, just contact me
manicaesar,
Do you know how to get the sounds to play from a file on a webserver, instead of just playing it from a sound on your iphone?

I would like to be able to put my songs on my webserver essentially and access it through my iphone app. Any help would be greatly appreciated!

Thanks
pmvinuelas is offline   Reply With Quote
Old 05-26-2009, 08:24 AM   #6 (permalink)
Registered Member
 
manicaesar's Avatar
 
Join Date: Feb 2009
Location: Poznań, Poland
Age: 25
Posts: 103
manicaesar is on a distinguished road
Default

Quote:
Originally Posted by pmvinuelas View Post
manicaesar,
Do you know how to get the sounds to play from a file on a webserver, instead of just playing it from a sound on your iphone?

I would like to be able to put my songs on my webserver essentially and access it through my iphone app. Any help would be greatly appreciated!

Thanks
Hi,
Sorry for my delay in responding you. But fortunately I was able to quickly find an answer to your question... Of course if you haven't already solved it

All you have to do is to get data from internet. You can do it this way:
In your class define some fields:
Code:
NSMutableData *data;
NSURLConnection *connection;
AVAudioPlayer *player;
Then in implementation you can use this code, to play the audio file:
Code:
- (void)getAudioFromNet {
	
	NSURL *url = [NSURL URLWithString:@"http://atos.wmid.amu.edu.pl/~d301627/OpSession2009.mp3"]; //Example URL to the funny song I've created some time ago ;)
	NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
	connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
	
}

- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
    if (data==nil) {
		data = [[NSMutableData alloc] initWithCapacity:2048];
    }
    [data appendData:incrementalData];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
	
	player = [[AVAudioPlayer alloc] initWithData:data error: nil];
	[player setVolume:0.1];
	[player play];
	
}
Worked for me
manicaesar is offline   Reply With Quote
Old 07-29-2009, 12:41 AM   #7 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 1
balaa is on a distinguished road
Default Thanks

Quote:
Originally Posted by manicaesar View Post
Hi,
Sorry for my delay in responding you. But fortunately I was able to quickly find an answer to your question... Of course if you haven't already solved it

All you have to do is to get data from internet. You can do it this way:
In your class define some fields:
Code:
NSMutableData *data;
NSURLConnection *connection;
AVAudioPlayer *player;
Then in implementation you can use this code, to play the audio file:
Code:
- (void)getAudioFromNet {
	
	NSURL *url = [NSURL URLWithString:@"http://atos.wmid.amu.edu.pl/~d301627/OpSession2009.mp3"]; //Example URL to the funny song I've created some time ago ;)
	NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
	connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
	
}

- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
    if (data==nil) {
		data = [[NSMutableData alloc] initWithCapacity:2048];
    }
    [data appendData:incrementalData];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
	
	player = [[AVAudioPlayer alloc] initWithData:data error: nil];
	[player setVolume:0.1];
	[player play];
	
}
Worked for me


Thank you for this.
balaa is offline   Reply With Quote
Old 08-27-2009, 03:00 AM   #8 (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 manicaesar View Post
Ok, I've already solved it by myself.

It's simple: you just have to release previous player and alloc and init it again. It does not work at first, because I do not retain NSData object which I used to init the player object. My code looks something like that:

Code:
- (void)playerPlay:(id)sender {
	int idx =[listOfResultsTableView indexPathForSelectedRow].row;
	[player stop];
	[player setCurrentTime:0.0];
	[player release];
	NSData *cdata = [Searcher getAudioDataForSearchResult:[searchResults objectAtIndex:idx]];
	if(cdata == nil) {
		NSLog(@"cdata nil");
		return;
	}
	player = [[AVAudioPlayer alloc] initWithData:cdata error: nil];
	[player setVolume:0.1];
	[player play];
}
If someone in future needs help, just contact me
Hi,
I am facing strange memory leak.
I have array of images loaded on to scrollview. When a image is scrolled onto mainview, i am playing corresponding sound file.

No problem with playing different sounds for different images.

But i am getting memory leaks.

In simulator GeneralBlock-240 and SegmentMachO

In Device GeneralBlock-3584

Could any one help me?

NSString *soundFileIs=[imageSoundArray objectAtIndexage];
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *audioFile = [paths stringByAppendingPathComponent:soundFileIs];
NSData *cdata =[NSData dataWithContentsOfFile:audioFile];
if(cdata == nil) {
NSLog(@"cdata nil");
return;
}
player = [[AVAudioPlayer alloc] initWithData:cdata error:nil];
[player retain];
[player setDelegate:self];
[player setVolume:1.0];
[player play];


i am releasing this player in delegate method.

- (void)audioPlayerDidFinishPlayingAVAudioPlayer *)player1 successfullyBOOL)flag
{
if (flag == NO)
NSLog(@"Playback finished unsuccessfully");
NSLog(@"delegate");
[player stop];
[player setCurrentTime:0.0];
[player release];
player=nil;
}

Please help me...
shiva.0537 is offline   Reply With Quote
Old 08-28-2009, 03:40 AM   #9 (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

Hi,

I get rid of memory leak in simulator, but still getting GeneralBlock-3584 in device.

It is pointing to [myAudioPlayer play]; line in my code.

could any one help me?

Thanks
shiva
shiva.0537 is offline   Reply With Quote
Old 09-11-2009, 03:44 AM   #10 (permalink)
Registered Member
 
finefin's Avatar
 
Join Date: Jun 2009
Posts: 259
finefin is on a distinguished road
Default

did you find a solution, shiva?
because I have a similar problem:
works like a charm in the simulator,
causes heavy memory overflows on the device.

any help would be appreciated.
finefin is offline   Reply With Quote
Old 09-11-2009, 07:39 AM   #11 (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 finefin View Post
did you find a solution, shiva?
because I have a similar problem:
works like a charm in the simulator,
causes heavy memory overflows on the device.

any help would be appreciated.
Hi,

No, i didn't get any solution for it. But i am not getting any leak in simulator, so i uploaded to app store, waiting for approval.

If apple also found same leaks, they will suggest na, then i will let you know.

Thanks
shiva
shiva.0537 is offline   Reply With Quote
Old 02-16-2010, 12:07 PM   #12 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 3
prophyt is on a distinguished road
Default

Quote:
Originally Posted by zhydenina View Post
Hi manicaesar,

I've been working on something similar. In my app with a tableview, I have about 10 table cells each to be associated with 10 different sounds (.mp3). When a user selects a different cell, a different sound will be played. The question is, how do I associate each table cell with each sound file? Do I store those sound files in an array at the same time I name each table cell? how could i specifically achieve this, Thanks for your help
Hey Zhydenina,
Did you find a solution for this problem? If so, could you please share (code sample)...? I am having the same issue. Thanks.
prophyt is offline   Reply With Quote
Old 03-08-2010, 03:22 AM   #13 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 13
sapple is on a distinguished road
Default

Quote:
Originally Posted by manicaesar View Post
Ok, I've already solved it by myself.

It's simple: you just have to release previous player and alloc and init it again. It does not work at first, because I do not retain NSData object which I used to init the player object. My code looks something like that:

Code:
- (void)playerPlay:(id)sender {
	int idx =[listOfResultsTableView indexPathForSelectedRow].row;
	[player stop];
	[player setCurrentTime:0.0];
	[player release];
	NSData *cdata = [Searcher getAudioDataForSearchResult:[searchResults objectAtIndex:idx]];
	if(cdata == nil) {
		NSLog(@"cdata nil");
		return;
	}
	player = [[AVAudioPlayer alloc] initWithData:cdata error: nil];
	[player setVolume:0.1];
	[player play];
}
If someone in future needs help, just contact me
Hi Manicaesar,

I am working on a DJ app for Iphone. For this I have to play multiple audio files at a time i.e. users could listen audio1,audio2,audio3,.... simultaneously.

Can we play multiple audio files with one instance of AVAudioPlayer or we would be requiring different AVAudioPlayer instances for each audio file.

Any help will be highly appreciated.

Thanks in Advance.

Gaurav Arora

Last edited by sapple; 03-08-2010 at 03:28 AM.
sapple is offline   Reply With Quote
Old 03-08-2010, 05:02 AM   #14 (permalink)
Registered Member
 
manicaesar's Avatar
 
Join Date: Feb 2009
Location: Poznań, Poland
Age: 25
Posts: 103
manicaesar is on a distinguished road
Default

Quote:
Originally Posted by sapple View Post
Hi Manicaesar,

I am working on a DJ app for Iphone. For this I have to play multiple audio files at a time i.e. users could listen audio1,audio2,audio3,.... simultaneously.

Can we play multiple audio files with one instance of AVAudioPlayer or we would be requiring different AVAudioPlayer instances for each audio file.

Any help will be highly appreciated.

Thanks in Advance.

Gaurav Arora
Hi, Guarav

Quick look at API Reference and I think there are responses to all your questions:
Quote:
An instance of the AVAudioPlayer class, called an audio player, provides playback of audio data from a file or memory.

Apple recommends that you use this class for audio playback unless your application requires stereo positioning or precise synchronization, or you are playing audio captured from a network stream. For an overview of audio technologies, see Getting Started with Audio & Video.

Using an audio player you can:

- Play sounds of any duration
- Play sounds from files or memory buffers
- Loop sounds
- Play multiple sounds simultaneously, one sound per audio player
- Control relative playback level for each sound you are playing
- Seek to a particular point in a sound file, which supports such application features as fast forward and rewind
- Obtain data that you can use for playback-level metering
The AVAudioPlayer class lets you play sound in any audio format available in iPhone OS. You use a delegate to handle interruptions (such as an incoming phone call) and to update the user interface when a sound has finished playing. The delegate methods for the AVAudioPlayer class are described in AVAudioPlayerDelegate Protocol Reference.

To play, pause, or stop an audio player, call one of its playback control methods, described in “Configuring and Controlling Playback.”

This class uses the Objective-C declared properties feature for managing information about a sound—such as the playback point within the sound’s timeline, and for accessing playback options—such as volume and looping. You also use a property (playing) to test whether or not playback is in progress.

To configure an appropriate audio session for playback, refer to AVAudioSession Class Reference and AVAudioSessionDelegate Protocol Reference. To learn how your choice of file formats impacts the simultaneous playback of multiple sounds, refer to “Playing Multiple Sounds Simultaneously” in iPhone Application Programming Guide.
In addition, you can look at Cocos2d for iPhone. It is an open source framework for creating 2D games on iPhone (written almost completely in ObjectiveC) and it has sub-engine called CocosDenshion which is an audio engine. I used CocosDenshion in my game (see signature) and it works very well.

Regards
--
ML
__________________
Do you like Age Of War? Have you played 'The Wars' and are not fully satisfied? Try Empires At War - we bet you will love it!


------------------
Full Version
------------------
Lite Version
------------------
YouTube Game Trailer
manicaesar is offline   Reply With Quote
Old 05-03-2010, 04:22 PM   #15 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 16
ufbobbo is on a distinguished road
Default

manicaesar,

would you mind looking into my issue with AVAudioPlayer not releasing memory after it's done playing? i'm really struggling here and could use the help: link is here

thanks in advance!
ufbobbo is offline   Reply With Quote
Reply

Bookmarks

Tags
avaudioplayer

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: 332
3 members and 329 guests
guusleijsten, Kryckter, LEARN2MAKE
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:49 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0