Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon


23. November 2015

 

 

In this tutorial we are going to look at audio programming in Cocos2d-x.  We will look at playing music and sound effects using SimpleAudioEngine, one of two engines built into Cocos2d-x.  There is a second, newer, more commplex and experimental audio engine AudioEngine, that we will discuss later.  Let’s start straight away by playing some music.  To make use of SimpleAudioEngine we need to add an additional include call:

#include "SimpleAudioEngine.h"

 

Next we need a song of some sorts to play, simply copy an appropriate file into your resources folder.  Myself I used an mp3 file named creatively enough song.mp3.

Supported Audio File Formats


The audio formats supported by Cocos2D-x depend entirely on what platform you run on.  The primary thing to be aware of is the ogg format is the preferred music format on Android platforms, while it is completely unsupported on iOS, which prefers MP3.

You should be aware that the MP3 format is patent encumbered format and generally should be avoided when possible.  If your app reaches certain sales thresholds, you may be required to pay license fees.  Sadly this generally isn’t an option on iOS devices as MP3 is the primary audio format used.  For sound effects, WAV are commonly used offering quick playback ability at the cost of file size.  Here are the details of supported audio files on iOS

Time now for some coding.  Implement the following init() method:

bool HelloWorld::init()
{
   if (!Layer::init())
      return false;

   auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
   audio->preloadBackgroundMusic("song.mp3");
   audio->playBackgroundMusic("song.mp3");

   return true;
}

That is all that is required to load and play a music file. In fact the preloadBackgroundMusic() call wasn't even required so we could have used even less code. However preloading your music guarantees that you will not suffer a slow down the first time a song plays. You can also pause and resume playback of background music, or switch tracks completely, like so:

   eventListener->onKeyPressed = [audio](EventKeyboard::KeyCode keyCode, Event* event) {

      switch (keyCode) {
         case EventKeyboard::KeyCode::KEY_SPACE:
            if (audio->isBackgroundMusicPlaying())
               audio->pauseBackgroundMusic();
            else
               audio->resumeBackgroundMusic();
            break;

         case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
            audio->playBackgroundMusic("song2.mp3");
            break;

         case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
            audio->playBackgroundMusic("song.mp3");
            break;
      }
   };

   _eventDispatcher->addEventListenerWithFixedPriority(eventListener, 2);

Hitting the spacebar will toggle the playback of the currently playing song.  Hitting the right arrow will start playing (or start over if already playing) song2.mp3. Hitting the left arrow will start or re-start playback of song.mp3.  You will notice from this example that only one song can be played at a time.  Generally this isn’t a limitation as it is normal to only have one active sound track at a time. 

setBackgroundMusicVolume() doesn't work!


A bit of a warning, at least on Windows, calling setBackgroundMusicVolume() does nothing, making it impossible to change the volume of a playing music file. This may not be the case on other platforms, I did not test. It was filed as a bug a long time back and does not appear to have been addressed.

 

Now let's look at playing sound effects instead.  Playing music and effects is almost identical.  The biggest difference is that sound effects are expected to support multiple concurrent instances.  That is to say, while you can only play one song at a time, you can play multiple sound effects at once. Consider this sample:

bool HelloWorld::init()
{
   if (!Layer::init())
      return false;

   auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
   
   audio->preloadEffect("gun-cocking-01.wav");
   audio->preloadEffect("gun-shot-01.wav");

   audio->playEffect("gun-cocking-01.wav");

   Director::getInstance()->getScheduler()->schedule([audio](float delta) {
      audio->playEffect("gun-gunshot-01.wav");
      audio->unloadEffect("gun-cocking-01.wav");
   }, this, 1.5f, 0, 0.0f, false, "myCallbackKey");

   return true;
}

 

In this example we preload two WAV sound effects, a gun cocking and a gun shot.  Playing a sound effect is as simple as calling playEffect() passing in the file name.  Of course, be certain to copy the appropriate sound files to your project’s resource folder before running this example.  Next this example queues up a lambda method to be called 1.5 seconds of the gun cocking sound is played to play our gun shot sound.  At this point we are done with our gun cocking effect so we unload it from memory using unloadEffect().  You can still call playEffect with that file in the future, but it will result in the file being loaded again.

 

This example might be somewhat convoluted, but it illustrates and works around a key weakness in the CocosDenshion audio library.  It is a very simple and straight forward library but if you want to do “advanced” things like detecting when a song or audio effect has ended, unfortunately this functionality is not available.  You either have to use the experimental AudioEngine, which we will cover later, or use an external audio library such as FMOD.  SimpleAudioEngine is extremely easy to use, but not very powerful, so it’s certainly a trade off.  If you just need background music and fire and forget sound effects SimpleAudioEngine should be just fine for you.

 

One final topic to cover is handling when your app is minimized or forced into the background, you most certainly want to stop audio playback.  This is thankfully easily accomplished in your AppDelegate there are a pair of methods, applicationDidEnterBackground() and applicationWillEnterForeground().  Simply add the following code:

void AppDelegate::applicationDidEnterBackground() {
   auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
   audio->pauseAllEffects();
   audio->pauseBackgroundMusic();
}

void AppDelegate::applicationWillEnterForeground() {
   auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
   audio->resumeAllEffects();
   audio->resumeBackgroundMusic();
}

 

This will cause all of your currently playing sound effects and music files to be paused when your application enters the background and they will all result when your application regains focus.

Programming , ,

blog comments powered by Disqus

Month List

Popular Comments

Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon

3. March 2017

 

GitHub is perhaps the most popular source repository available today, while Unity is the most used game engine available today.  That said, neither ever worked particularly well with the other.  Today at GDC GitHub announced a new extension aimed at changing that.  The GitHub for Unity extension plugs directly into Unity and enables programmers and artists to work with Unity.

 

Details from the Github announcement:

Git helps millions of developers write and collaborate on code, but it's not always a practical choice for building games. With the GitHub for Unity extension, Unity game developers (artists and programmers alike) can better integrate Git and GitHub into their workflow, even if they're versioning large binary assets.

The GitHub for Unity extension integrates Git and GitHub directly into the Unity Editor. You can easily configure, collaborate, and manage your Git project in a dedicated window. The extension also includes Git LFS v2.0 support to store large binary assets and introduces file locking to help communicate with your team that you are working on difficult-to-merge files.

unity-screenshot

The GitHub for Unity extension is a first step towards unifying the GitHub and Unity workflows, and we'd love to hear your feedback to help guide us in the right direction. Watch for an alpha release over the next few weeks. We'll be making the project open source and publishing the extension in the Unity asset store soon after.

Many thanks to Emil "AngryAnt" Johansen for all his help in getting this project up and running.

Sign up now to get access to the GitHub for Unity plugin preview.

As you may have noticed from the last line, the release is a preview only at this point, so expect some bugs and warts for now.

GameDev News

2. March 2017

 

A new update has been released for CryEngine, bringing it to version 5.3.3.  Primarily a bug fix release, it also adds a new C# game template Rolling Ball.

 

From the release notes:

Game Templates


New: Added RollingBall template to the C# game template selection.

Refactored: Removed the Sydewinder application - this will be maintained as a separate project and is available as a download from the CRYENGINE Marketplace.

Audio


Audio General

Fixed: Bug where the MS compiler auto-vectorizer produced illegal instructions in mpeg code.

Core/System


Engine General

Fixed: Added ScaleformHelper to builds.

Fixed: Download of 5.3.1 SDKs package.

Fixed: Crash during Scaleform shutdown.

System

Fixed: Sydewinder doesn't close properly in Launcher.

Fixed: Encoding fixes for command line parsing.

Fixed: Crash reporter not producing crash dumps on Windows 7.

WAF

Fixed: ScaleformHelper usage by WAF (all configs) and CMake (release config).

Tweaked: Don't monolithically link MonoBridge into release configs, it's an optional feature.

Tweaked: Copy the right portaudio binaries (performance and release config).

CMake

Optimized: Don't copy unnecessary debug DLLs or PDBs to bin/win_x64.

Fixed: Compiler detection for VS2017. 

Action General

Fixed: The ability to load a second level after the first has been loaded.

C#

Fixed: The Input class not registering mouse -movement.

GameDev News

1. March 2017

 

Today at GDC 17, Microsoft announced the preview launch of Xbox Live Creators Program.  This is a new SDK and developer program that enables anyone to quickly publish their title to Xbox One or Windows 10 after a quick certification process.  Similar in many was to the ID@Xbox program, the following chart breaks down the key differences.

image

 

More details of the new program from the announcement:

Xbox Live will soon be open to all developers via the Xbox Live Creators Program – no concept approval required! You will be able to rapidly publish your game to Xbox One or Windows 10 through a short and simplified certification process.

Integrate Xbox Live social experiences such as sign-in, presence, leaderboards, and more into your title, with minimal development time. Xbox Live social features are designed to organically grow your audience, spreading awareness to over 55 million active gamers. The full set of Xbox Live capabilities is available via the ID@Xbox program and a table comparing the features is below.

Using tools you already use, and your existing Xbox One retail console, you can easily create or leverage existing code to develop your title. Supported game engines include Construct 2, MonoGame, Unity, and Xenko. We anticipate more game engines and tools to support Xbox Live Creators Program over time. Please check with your preferred game engine for their support of the Creators Program.

On Xbox One, which offers gamers a curated store experience, games published through the Xbox Live Creators Program will be available in the new, "Creator games section" within the store. On Windows 10 PC, games in Xbox Live Creators Program will be visible with the other games in the Windows Store.

 

Wouldn’t have been great if Microsoft never gave up on XNA in the first place?  Still one of their most idiotic moves historically.  It is my belief that this program will live or die on the quality of the curation.  This does however offer a new and interesting avenue for indie developers, so could be a great new opportunity.

GameDev News

1. March 2017

 

MonoGame is a popular open source port of Microsoft’s now defunct XNA game framework.  They just announced the release of version 3.6 containing several improvements and fixes.  If you are interested in learning MonoGame, be sure to check out our complete tutorial series available here.  MonoGame has been used to create several of the biggest indie games on the market, including Stardew Valley, Braid and many more.

 

Details of the release from the changelog:

3.6 Release - 2/28/2017

  • Fixed XML deserialization of Curve type. #5494
  • Fix #5498 Pipeline Tool template loading on MacOS. #5501
  • Fix typo in the exclude.addins which cause warnings when installing the Addin in XS. #5500
  • Added support for arbitrary defines passed to the Effect compiler. #5496
  • Fixed GraphicsDevice.Present() to check for current render target. #5389
  • Custom texture compression for SpriteFonts. #5299
  • Performance improvements to SpriteBatch.DrawString(). #5226
  • Removed the OUYA platform #5194
  • Dispose of all graphical resources in unit tests. #5133
  • Throw NoSuitableGraphicsDeviceException if graphics device creation fails. #5130
  • Optimized and added additional constructors to Color. #5117
  • Added SamplerState.TextureFilterMode to correctly support comparison filtering. #5112
  • Fixed Apply3D() on stereo SoundEffect. #5099
  • Fixed Effect.OnApply to return void to match XNA. #5090
  • Fix crash when DynamicSoundEffectInstance not disposed. #5075
  • Texture2D.FromStream now correctly throws on null arguments. #5050
  • Implemented GraphicsAdapter for DirectX platforms. #5024
  • Fixed initialization of GameComponent when created within another GameComponent. #5020
  • Improved SoundEffect internal platform extendability. #5006
  • Refactored audio processing for platform extensibility. #5001
  • Refactored texture processing for platform extensibility. #4996
  • Refactor ShaderProfile to allow for pipeline extensibility. #4992
  • Removed unnessasary dictionary lookup for user index buffers for DirectX platforms. #4988
  • New SetRenderTargets() method which allows for variable target count. #4987
  • Added support for XACT reverb and filter effects. #4974
  • Remove array in GamePadDPad constructor. #4970
  • Updated to the latest version of Protobuild. #4964
  • Fixed static VBs and IBs on UWP on XB1. #4955
  • Updated to the latest version of Protobuild. #4950
  • Update Xamarin Studio addin for latest platform changes. #4926
  • Replace OpenTK with custom OpenGL bindings #4874
  • Fix Mouse updating when moving the Window. #4924
  • Fix incorrect use of startIndex in Texture2D.GetData DX. #4833
  • Cleanup of AssemblyInfo for framework assembly. #4810
  • New SDL2 backend for desktop GL platforms. #4428
  • Two MaterialProcessor properties fixed. #4746
  • Fixed thumbstick virtual buttons to always use independent axes. #4742
  • Fixed back buffer MSAA on DirectX platforms. #4739
  • Added new CHANGELOG.md to project. #4732
  • Added obsolete attribute and updated documentation. #4731
  • Fixed layout of UWP windows in VS template to ignore window chrome. #4727
  • Remove support for reading raw assets through ContentManager. #4726
  • Implemented DynamicSoundEffectInstance for DirectX and OpenAL platforms. #4715
  • Removed unused Yeti Mp3 compressor. #4713
  • MonoGame Portable Assemblies. #4712
  • Fixed RGBA64 packing and added unit tests. #4683
  • Fix Gamepad crash when platform doesn't support the amount. #4677
  • Fixed Song stopping before they are finished on Windows. #4668
  • Removed the Linux .deb installer. #4665
  • OpenAssetImporter is now automatically selected for all the formats it supports. #4663
  • Fixed broken unit tests under Linux. #4614
  • Split out Title Container into partial classes. #4590
  • Added Rider Support to Linux installer. #4589
  • Implement vertexStride in VertexBuffer.SetData for OpenGL. #4568
  • Performance improvement to SpriteBatch vertex generation. #4547
  • Optimization of indices initialization in SpriteBatcher. #4546
  • Optimized ContentReader to decode LZ4 compressed streams directly. #4522
  • TitleContainer partial class cleanup. #4520
  • Remove raw asset support from ContentManager. #4489
  • Initial implementation of RenderTargetCube for OpenGL. #4488
  • Removed unnecessary platform differences in MGFX. #4486
  • SoundEffect fixes and tests. #4469
  • Cleanup FX syntax for shader compiler. #4462
  • General Improvements to Pipeline Gtk implementation. #4459
  • ShaderProfile Refactor. #4438
  • GraphicsDeviceManager partial class refactor. #4425
  • Remove legacy Storage classes. #4320
  • Added mipmap generation for DirectX render targets. #4189

In related MonoGame news, Nintendo Switch support was recently announced via Twitter:

image

GameDev News

1. March 2017

 

Kotlin is a new programming language created by JetBrains, the folks behind such great developer tools as IntelliJ, WebStorm and Resharper.  It is a statically typed language the runs in the JVM, on Android and as of this release, compiled to JavaScript.  Kotlin was designed to be a lot more concise than Java, one of the biggest pain points people have with the language.  It is also meant to be 100% interoperable with existing Java code and frameworks.  Today’s release also brings JavaScript support out of “experimental” status, now enabling Kotlin to compile down to JavaScript much like other languages such as TypeScript and Dart.  This release also brings CoRoutine support for easier async programming.

 

From the announcement blog post:

Our vision for Kotlin is to enable the use of a single expressive, performant, strongly typed language across all components of a modern application. Kotlin 1.1 makes two major steps towards this goal.

First, the JavaScript target is no longer experimental, and supports all Kotlin language features, a large part of the standard library, as well as JavaScript interoperability. This allows you to migrate the browser frontend of your applications to Kotlin, while continuing to use modern JavaScript development frameworks such as React.

Second, we’re introducing support for coroutines. As a lightweight alternative to threads, coroutines enable much more scalable application backends, supporting massive workloads on a single JVM instance. In addition to that, coroutines are a very expressive tool for implementing asynchronous behavior, which is important for building responsive user interfaces on all platforms.

Below we describe these two changes further. In other news: we’ve added type aliases, bound callable references, destructuring in lambdas and more. See the details in our What’s new page (check out the runnable examples!).

 

Kotlin is available as a plugin for IntelliJ IDEA, NetBeans and Eclipse.

GameDev News

Month List

Popular Comments