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
Related
We have a VOIP application, that records and plays audio. As such, we are using PlayAndRecord (kAudioSessionCategory_PlayAndRecord) audio session category.
So far, we have used it successfully with iPhone 4/4s/5 with both iOS 6 and iOS 7 where call audio and tones played clearly and were audible.
However, with iPhone 5s, we observed that both the call audio and tones sound robotic/garbled in speaker mode. When using earpiece/bluetooth/headset, sound is clear and audible.
iOS Version used with iPhone 5s: 7.0.4
We are using audiounits for recording/playing of call audio.
When setting audio properties like session category, audio route, session mode etc., we tried both the older (deprecated) AudioSessionSetProperty() and AVAudioSession APIs.
For playing tones, we are using AVAudioPlayer. Playing of tones during the VOIP call and also when pressing keypad controller within the app produces robotic sound.
When instantiating the audio component using AudioComponentInstanceNew, we set componentSubType to kAudioUnitSubType_VoiceProcessingIO.
When replacing kAudioUnitSubType_VoiceProcessingIO with kAudioUnitSubType_RemoteIO, we noticed that the sound of call audio and tones was no longer robotic, it was quite clear, but the volume level was very low when using speaker mode.
In summary, keeping all the other audio APIs the same:
kAudioUnitSubType_VoiceProcessingIO: Volume is high (desirable) but sound of tones and call audio was robotic in speaker mode.
kAudioUnitSubType_RemoteIO: Sound of tones and call audio was clear but it is not audible.
STEPS TO REPRODUCE
- Set audio session category to playAndRecord.
- Set audio route to speaker
- Set all the other audio properties like starting audio unit, activating the audio session, instantiating the audio components.
- Set the input and render callbacks
- Try both options
1. Play tones using AVAudioPlayer
2. Play call audio
Any suggestions on how to get over this issue. Raised as an issue with Apple but no response yet from them.
i have shared the code here github link
The only difference between kAudioUnitSubType_VoiceProcessingIO and kAudioUnitSubType_RemoteIO is that voiceProcessing includes code to tune out acoustic echo i.e. tunes out the noise from the speaker so the microphone doesn't pick it up. Its been a long time since I've played with the audio framework but I remember that to sound off there could be any number of things,
Are you doing any work in the audio callbacks that could be taking a long time?
The callbacks run on realtime threads. if your processing takes too long you can miss data. Would be helpful to track the data over a fixed period of time to see are you capturing it all. Use something like wireShark to sniff the network. Record the number of packets and see did the phone capture the same.
Are you modifying any of the audio?
Do you have a circular buffer that might be causing an issue?
I've had several issues doing this and one was using a third party circular buffer that was described as low latency and efficient ... it wasn't. I answered my own question here and included my circular buffer implementation that greatly improved my audio as the issue was I was skipping data.
Give this a go and let me know:
iOS UI are causing a glitch in my audio stream
Please be aware that some of this code is unique to the audio format ALaw, 0xD5 is a byte of silence in ALaw, if you are using linear PCM or any other that will probably be a noise of some kind.
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
If yes, what movie format has best performance? And how would a simple setup look like? I have some views, and I want to play a short movie inside a view (not fullscreen). The movie is about 5 seconds long.
Looks like the system frameworks only support playing video full screen with the MPMoviePlayerController. Supported formats are basically flavors of H.264 and MPEG-4; more in the documentation.
Theoretically, you might be able to roll your own decoding and playback code, but I doubt you'd get acceptable performance. (And most of the Open Source media player examples I can think of are GPL. Not that I imagine they'd fare much better.)
If it's only 5 seconds long, you can fake it by playing the audio file in the background, and an animated UIImageView at the same time. Lots of apps do this like the ~50 or so baby sign language ones.
Is there an easy way to control the playback speed/tempo of a sound file loop played using Audio Queue Services? For example, if a game is playing background music, I want to make the BGM speed up as time runs out, but without changing the pitch of the music. Thx!
There's no trivial way to do this that I know of. On the Mac, you'd presumably use Audio Units to do this, but I think the support for those is limited on the iPhone SDK.
You can do some processing during your AudioQueue playback callback, between AudioFileReadPackets() and AudioQueueEnqueueBuffer() but I think speed-shifting without changing the tone will require rather a lot of CPU time.
It'd probably be easier to have more than one recording of the music, and switch files during a silent passage of the music.
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+.