Get currently playing Mix_Music with SDL_Mixer - c++

I'm implementing sound in my C++ game with SDL_Mixer, and can't find a function in the manual that would give me the Mix_Music that is currently playing. I figure I need it to know whether I can free the Mix_Music or not, depending on whether it is currently playing.

According to https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC56 you can simply free the music whether it is playing or not. The music will halt if it is playing, or finish fading out if it is fading out.
You had better just remember the last music you asked to be played yourself if you want to keep track of it for other reasons.

Related

How to Halt Sound Effect in SDL2

How would I halt the playing of a sound effect in SDL2?
Currently I'm playing sound effects using the SDL2 Mixer with this code.
Mix_PlayChannel(-1, soundEffect, 0);
However I want the play to be able to not have to listen to the entire sound effect and when they leave the menu the sound effect should stop.
I've tried Mix_HaltMusic(); however that doesn't seem to apply to Mix_Chunk.
How would I do so?
To stop the Mix_Chunk started with Mix_PlayChannel, you have to use Mix_HaltChannel as explained in this answer for the opposite problem.

XAudio2 delayed sound - when playing multiple voices

I am using XAudio2 to play sounds in my game. I have about 16 source voices out of which 3 are looping effects which need to be played just like a background music. I have another background music which starts playing at regular intervals(once completed it waits for 1 minute and restarts). Whenever another effect has to be played like button click, gun shot etc, I search for an available source and attach the buffer to it and starts playing.
My problem is once the background music has started playing, just the looping effects will get played and other effects which need to be played on game actions(such as button click) do not get played. And all of these delayed effects get played once the background music is stopped. Also if I use one looping effect(instead of 3) all these get played in the right order.
I am using a submix voice for effects and background music is submitted directly to the mastering voice.
Can someone please help me as I am stuck here
Whenever one intends to play different sounds in the same time, he or she should make sure that these sounds are not in a queue, displayed in a sequential order, but they are displayed using different threads.
In Milsim's case the problem was exactly this, apparently, he used tasks to display the sounds, but they were not issued in different threads. He solved his problem using IAsyncAction.

cocos2d-iphone games stop playing background music sometimes

cocos2d-iphone 1.0.1.
I have noticed this with other cocos2d-iphone games installed in my device, like Kingdom Rush.
Basically, most of the times, the audio is fine (almost always). But suddenly, at an unexpected moment, the background music stops playing and only the sound effects work. Sometimes, killing the application will not be enough to fix it.
With my cocos2d-iphone game this happens as well, with no hint in the console. I use SimpleAudioEngine to play background music and sound effects.
Killing my application, restarting Xcode will not fix it. I usually just ignore the problem and, in the near future, it is suddenly gone. I suspect that rebooting the device tends to fix this, but that's beyond the point: I should know why is it happening.
I also tried preloading my background music. Doesn't change a thing.
I believe I have experienced this problem with both .mp3 and .wav formats.
Why might this be happening?
No idea to the exact reason, but I can think of a few:
memory warning causes audio stream to be interrupted
audio interruptions (calendar notification, incoming SMS/call) not handled properly by CocosDenshion
other streaming music is played (ie perhaps videoplayer, iPod music player)
music isn't streamed but buffered, which means music is fighting over audio buffers with all other effects - eventually so many audio effects are played that older buffers have to cancel playback (which might be the music) in order to allow the new effect to play
defective device (since it happens in other apps …)
bug in CocosDenshion (check the cocos2d issue tracker and forum for any unresolved audio bugs)
I think you can exclude the latter if you're using the playBackgroundMusic API to stream music instead of buffering it.

Looping sound effects in CocosDension, Cocos 2d

CocosDension's SimpleAudioEngine offers way to loop background music. But only one music file can be played at a time. I need to loop multiple sound files. Is there an easy way to achieve this using CocosDension of Cocos 2d?
If you use the CDSoundSource you can set if you want to loop or not the sound.
Yes define a CDSoundSource for each sound and play independently.
Check the samples in the CocosDenshion folder, specifically FadeToGrey/TheAudioCode class.
You can also use the
SimpleAudioEngine::playEffect(soundfile, true)
This will play the effect in a loop

Large number of Sound Effects with SimpleAudioEngine

Background:
My background music is being played as a sound effect because I want to change the pitch between each repetition of the tune.
The Issue:
While the background music is playing there's a lot of other short sound effects happening. After a certain number of sound effects play, my background music (also a sound effect) cuts out. The queue seems to cycle so that after say 50 sound effects have played, when the 51st plays, the 1st will be released whether it has completed or not.
Request for direction:
There are two directions I can see possibly going with this issue.
1. Not play the background music as an effect and figure out how to change the pitch as background music instead of an effect
2. Some how make sure that the effect will be retained until it has completed.
Thanks
The SimpleAudioEngine doesn't allow you to control channels, so it'll just play your sound effects in one of the channels it has allocated internally. Eventually all channels are playing audio. In that case, when a new audio file is to be played and all channels are already playing, SimpleAudioEngine will cancel one of the existing audio files. On occasion this will be your background music. Dang.
What you normally do to fix this is to assign (allocate) one audio channel specifically for the background music. No other audio will ever attempt to play audio on that channel. You will have to use the "regular" CocosDenshion API. It looks like you either need to use CDAudioManager or CDSoundEngine, but I have no experience with using channels with CocosDenshion.
Personally I never used CocosDenshion, whenever I needed more control I found the ObjectAL API to be easier to understand and it has excellent documentation. I made the following helper function for ObjectAL that plays a sound on a particular channel. I simply instantiated a ALChannelSource for my background music, and only played background music through that channel. The returned ALSoundSource even allows you to change pitch, pan, etc. while the audio is playing.
+(id<ALSoundSource>) playEffect:(NSString*)effect channel:(ALChannelSource*)channel loop:(bool)loop
{
id<ALSoundSource> soundSource = nil;
if (channel)
{
ALBuffer* buffer = [[OALAudioSupport sharedInstance] bufferFromFile:effect];
soundSource = [channel play:buffer loop:loop];
}
return soundSource;
}
All other audio files I'm playing through the OALSimpleAudio class.