How can i use more them one sound simultainiously in iphone application using cocos2d? - iphone

Duplicate:
use more then one sound in my iphone application using cocos2d
Hi,
All of the member of this forums.I need to help yours for doing my game project.I want to play some sound in my gaming project.When this game is started a background sound is playing continuously until the game is stop (exit). When game is played that time some action button are work and every action one sound is play for few second but background sound did not stop that time. How can i do this if anyone have a solution than reply with answer please.

If you are using cocos2d 0.82, then you can use denshion for this.
Play background music:
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:#"somemusic.mp3"];
Play SOUND EFFECT:
[[SimpleAudioEngine sharedEngine] playEffect:#"mysound.wav"]
You can find this and more at http://www.cocos2d-iphone.org/wiki/doku.php/cocosdenshion:cookbook

You could, if you implemented your own mixer. Just playback a buffer and mix the audio yourself. It' basically boils down to just averaging two different amplitudes. In some cases it's a bit more advanced.

Check out the AVAudioPlayer class. It will do what you need.
One thing to be aware of is that this class will only play one mp3 format sound at one time. I suggest converting to IMA4 format if you are using mp3 and need to play multiple sounds at once.
You can use the afconvert command to convert between sound file formats.
AVAudioPlayer is available in firmware 2.2+.

Related

iOS Coco2D playing 2 background sounds

Can Coco2D playing 2 background sounds?
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:#"sound1.caf" loop:YES];
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:#"sound2.caf" loop:YES];
Can sound2 be set to half of the volume and sound1 be the dominant?
It is highly discouraged to play two BGM (Background Music) at the same time. Because of the iPhone's hardware architecture, it has only one hardware sound decoder to play compressed music files.
With that said, your sounds seem to be in caf format, which is uncompressed, and thus easy to play using playEffect:. You can play as many effects as you want at the same time, without worrying about performance that much.
NOTE: The playEffect: method should only be used with uncompressed audio, such as caf, aif, ... etc.
From Steve Oldmeadow, author of CocosDenshion, the audio engined shipped with cocos2d.
There is a single AVAudioPlayer that plays background music so you can only have one background music file loaded at a time, if you need something more sophisticated then you have to roll your own solution.
Interesting links
http://www.cocos2d-iphone.org/forum/topic/3074
Cocos2d play 2 different background music files or loop playEffect

Cocos Denshion: Play sound effect in sync with music

I am making a music game and when the user presses a note it will produce a sound. The sound naturally needs to play immediately when the user presses, so they can tell whether they are in time with the music. However, it feels as if the sound is lagging, especially when note presses become quicker.
My background .m4a music file is played with AVAudioPlayer. I chose to use this over Cocos Denshion as I have access to the currentTime property. I may be wrong, but I dont think I can access this with CocosDenshion.
I made a .wav file which is extremely short (less than a second). I preload my sound effect on init:
[[SimpleAudioEngine sharedEngine] preloadEffect:#"Assist.wav"];
Then to play the sound effect, in CCTouchesBegan I call:
[[SimpleAudioEngine sharedEngine] playEffect:#"Assist.wav"];
After that it calls my code to determine the users timing and awards points. Any idea why it might be lagging, or a better way to play sound effects in time with music?
EDIT: Ive tried a few things recently with no results. First I tried playing the sounds automatically as they came up to the appropriate time in the song. Still had the lag, so I dont think it is touch events being slow. I also tried 3 different sound libraries.
However, when I ran in the simulator, it seemed to not be laggy. Does anyone have an idea? Im clueless and its a major feature I cant really take out...
you should avoid this code:- [[SimpleAudioEngine sharedEngine] preloadEffect:#"Assist.wav"];
with the start of app you should load your framework SimpleAudioEngine by writing this code :-
//SimpleAudioEngine *palySound; made object in .h file.
palySound=[SimpleAudioEngine sharedEngine];
and whenever you want to play sound you can write: [palySound playEffect:#"Assist.wav"];
I am not sure what you're doing in your SoundEngine, but in my own experience, the best way to not get lag to play a sound is to assign an AVAudioPlayer for each sound file (unless you want to start messing around with AudioQueues).
Here it is an example:
Let's assume that you have an AVAudioPlayer *assistPlayer; in your current view controller.
In your viewDidLoad initialize it with your sound:
NSURL *wavURL = [[NSBundle mainBundle] URLForResource:#"Assist" withExtension:#"wav"];
assistPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:wavURL error:nil];
Then, in your IBAction where you want to play the file, just do:
[assistPlayer play];
You shouldn't get any lag.
Did you try Finch? It claims to play sounds with low latency, and it is also just a wrapper around OpenAL.
Other than that, I'm really not experienced with OpenAL, but can think of two possible reasons for your lag:
The main thread is too busy - Try to offload work from it to other
threads.
Perhaps OpenAL is defined with too large of a buffer, so the pipeline loads the entire sound into the buffer (or a big chunk of it), and only afterwards the playback starts.

Access an mp3 in Cocos2D

I am playing a song in Cocos2D iphone using this line.
[[SimpleAudioEngine sharedEngine] playEffect:#"song.mp3"];
Suppose the mp3 is 1 min long, I want to play the song from 10 sec to 20 sec. Is it possible in Cocos2D iphone. Please help.
I don't think it is possible to "play the song from 10 sec to 20 sec" using the audio engine of cocos2d-iphone (which is named CocosDeshion).
SimpleAudioEngine is the main interface to CocosDeshion. Basically in cocos2d development we use audio files in two way - sound effect (short) and background music (long and usually loop). playEffect: is for the short ones, while playBackgroundMusic: and playBackgroundMusic:loop: is for the long ones. preloadBackgroundMusic: provides you the ability to cache the music in memory to avoid lag while playing, since they are often in compressed format.
As you can see in the documentation of SimpleAudioEngine (link above), there isn't such method for you to play a part of an audio file. Even the underlying class CDAudioManager doesn't support this.
I suggest to extract the 0:10 - 0:20 part as a separate audio file. But if you have a special reason to do this, you may want to use audio playback interfaces (such as AVAudioPlayer) in iOS SDK directly. Please refer to: http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html

Play a mp3 in slow/fast

I want to add to my app the ability to play songs in slow\fast motion.
from a research i done it can be done in two way in iphone :
OpenAL and soundtouch.
I am looking for something that can help add one of them to my project and to use them.
I had the same problem and just resolved in the following way
If you have any questions I can help you
Install cocos2d http://www.cocos2d-iphone.org/download
and with this you can play fast or slow file
[[SimpleAudioEngine sharedEngine] playEffect: (NSString *) pitch (float32) pan (float32) gain (float32)]

Fade In and Out Audio in iPhone SDK

I am wondering if there is some way to fade in and out audio using AVAudioPlayer in CocoaTouch, with a pause command once the music finishes fading and then a play command once the fading begins. I have heard that one can use NSTimer for this purpose, but I am unsure of the easiest way to accomplish this.
Thanks for any help!
There is a good video tutorial to do this here: Playing a sound, and fading a sound using AVAudioPlayer on iPhone.