Book Image

Cocos2d-x Cookbook

By : Akihiro Matsuura
Book Image

Cocos2d-x Cookbook

By: Akihiro Matsuura

Overview of this book

Table of Contents (18 chapters)
Cocos2d-x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Playing background music and a sound effect by using AudioEngine


AudioEngine is a new class from Cocos2d-x version 3.3. SimpleAudioEngine cannot play multiple background scores, but AudioEngine can play them. Furthermore, AudioEngine can call a callback function when it finishes playing the background music. In addition, we can get the playtime by using the callback function. In this recipe, we will learn more about the brand new AudioEngine.

Getting ready

We have to include the header file of AudioEngine to use it. Further, AudioEngine has a namespace called experimental. To include the header file, you will need to add the following code:

#include "audio/include/AudioEngine.h" USING_NS_CC; 
using namespace experimental;

How to do it...

AudioEngine is much easier than SimpleAudioEngine. Its API is very simple. The following code can be used to play, stop, pause, and resume the background music.

// play the background music 
int id = AudioEngine::play2d("sample_bgm.mp3"); 
// set continuously...