Cocos Denshion: Play Sound Effect in Sync with Music

I am making a music game, and when the user clicks a note, it will produce a sound. The sound, of course, should be played immediately when the user clicks, so they can determine if they were in time with the music. However, the sound seems to be lagging, especially when notes become faster.

My background .m4a music file is played using AVAudioPlayer. I decided to use this over Cocos Denshion, as I have access to the currentTime property. Maybe I'm wrong, but I don't think I can access this with CocosDenshion.

I made a wav file that is very short (less than a second). I preload the sound effect on init:

[[SimpleAudioEngine sharedEngine] preloadEffect:@"Assist.wav"]; 

Then, to reproduce the sound effect, in CCTouchesBegan I call:

 [[SimpleAudioEngine sharedEngine] playEffect:@"Assist.wav"]; 

After that, he calls my code to determine points in time and points for users. Any idea why this might be lagging, or the best way to play sound effects in time with music?

EDIT: Ive tried a few things lately without any results. At first I tried to play the sounds automatically when they came to the right time in the song. There was still a lag, so I don’t think it is a touch that is slow. I also tried 3 different sound libraries.

However, when I ran in the simulator, it did not seem laggy. Does anyone have an idea? Im clueless and its main feature that I can not really bear ...

+6
source share
3 answers

you should avoid this code: - [[SimpleAudioEngine sharedEngine] preloadEffect: @ "Assist.wav"];

with the start of the application, you should download your SimpleAudioEngine infrastructure by writing this code: -

// SimpleAudioEngine * palySound; the made object in the .h file. palySound = [SimpleAudioEngine sharedEngine];

and whenever you want to play a sound, you can write: [palySound playEffect: @ "Assist.wav"];

+1
source

I'm not sure what you are doing in your SoundEngine , but in my own experience the best way to not get a lag for playing sound is to assign AVAudioPlayer for each sound file (if you don't want to start messing with AudioQueues).

Here is an example:

Suppose you have AVAudioPlayer *assistPlayer; in your current 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, simply do:

 [assistPlayer play]; 

You must not fall behind.

0
source

Have you tried Finch ? He claims to play low-latency sounds, and it's also just a wrapper around OpenAL.

Other than that, I'm really not familiar with OpenAL, but I can think of two possible reasons for your lag:

  • The main thread is too busy - try disconnecting work from it to other threads.

  • Perhaps OpenAL is determined by too much buffer, so the pipeline loads all the sound into the buffer (or a large chunk), and only after that starts playback.

0
source

Source: https://habr.com/ru/post/905510/


All Articles