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.
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:
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
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.
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.
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:
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:
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:
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:
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.
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.
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:
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.
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!
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