How to add Vibration to android game

How to add Vibration to android game
0.0 0


Some methods for using JNI have changed in the 1.0.1-x-0.11.0 version of cocos2dx. So I decided to adjust the methods to the file SimpleAudioEngineJni.cpp. This was the only file I modified to get vibration to work again. The rest of the files I just copied over, but I will give the instructions on how to get Vibration on your game in this post. Most of it will be from the another post I wrote the instructions on.

I edited 7 files in total. Here are the instructions of what to do:

  1. You must edit the AndroidManifest.xml file which is located in “/android/”

Add the following line:

This will allow your app to use vibration.

  1. I modified 6 other files which are in the zip file I attached to this post. Here’s where each file goes:

2.1 Both and are generated in your project directory everytime you run the create-android-project.bat file. You must place and and into:

The modifications I added were adding 3 methods; vibrate(long time), vibrateWithPattern(long[] pattern, int repeat), cancelVibrate().
2.2 Next two files are SimpleAudioEngineJni.h and SimpleAudioEngineJni.cpp. These two files go into:

The modifications are the addition of 3 new methods; vibrateJNI(long long time), vibrateWithPatternJNI(long long pattern[], int repeat), cancelVibrateJNI().

2.3 The next file is SimpleAudioEngine.h. This goes into:


The modifications are declaring the 3 new methods; vibrate(long long time), vibrateWithPattern(long long pattern[], int repeat), cancelVibrate()

2.4 The last file is SimpleAudioEngine.cpp. This goes into:


The modifications are adding the 3 new methods; vibrate(long long time), vibrateWithPattern(long long pattern[], int repeat), cancelVibrate().

  1. To add vibration to your game, all you need to do is call the methods like so:

3.1 Vibration with the variable time which is a long type integer to determine how long the vibration lasts for:


The variable time is a measurement of milliseconds.

3.2 VibrationWithPattern with two variables; Pattern which is a array of long integers which is measured in milliseconds, and Repeat which is an interger:

Pattern tells the vibrator when to turn on or off. The first integer in the array tells the vibrator how long to wait before turning on and the next interger tells the vibrator how long to stay on before switching off. Example would be which would wait 1 millisecond before switching on and will last 2 seconds before switching off. The array can be as big as you want it. The sequence will be as described e.g. . Repeat basically tells the vibrator how long to repeat this pattern, or
1 if you don’t want it to repeat.

3.2 cancelVibrate turns off the vibrator:


The methods were created with intention of keeping the same structure of the vibrate methods from the android sdk: (10.9 KB) (12.3 KB) (12.3 KB)

How to implement vibration into the game?

Thanks Ray for your awesome work.
I’ve linked this post in [[FAQ]] page.
But it would be better to create a new class besides SimpleAudioEngine, e.g. SimpleVibrationEngine? :slight_smile:


it is greate! thank u.


the new beta changed some files, so here is an updated version working with the 0.13.0-beta.


NOTE: this is for cocos 0.13.0-beta

Right I’ve split the android vibration into a separate class. To call the methods such as vibrate for 100 milliseconds, it would be:

AndroidEffectsEngine::sharedEffectsEngine()->vibrate(100) (NOTE: it still uses the cocosdenshion namespace)

The methods are removed from the SimpleAudioEngine.h and SimpleAudioEngine.cpp and placed into the AndroidEffectsEngine.h and AndroidEffectsEngine.cpp.’ has been changed to add the ‘AndroidEffectsEngine.cpp’ to the library libcocosdenshion when compiling

I’ve removed the vibrate methods from the ‘’ Class and placed them into a new Class called ‘Cocos2dxAndroidEffects.Java’

I’ve modified ‘’ to initiate the object AndroidEffects as a instance of Cocos2dxAndroidEffects and called the methods from the instance

I have enclosed 7 files:

‘AndroidEffectsEngine.h’ and ‘SimpleAudioEngine.h’ goes into the “$cocosdirectory\CocosDenshion\include” folder

‘AndroidEffectsEngine.cpp’ and ‘SimpleAudioEngine.cpp’ and ‘’ goes into the “$cocos2dxRootdirectory\CocosDenshion\android” folder.

‘’ and ‘’ and ‘’ goes into the “$cocos2dxRootdirectory\tests\\src\org\cocos2dx\lib” folder. The reason why I place it into this folder, so that when I build a new project the new/altered java files will be copied over as well. Likewise, if you want to add it into your current project then just add the files into “$cocos2dxRootdirectory\$projectFolder\android\src\org\cocos2dx\lib” folder

‘game dev’ has already uploaded the JNI files that need to be changed if your working from cocos2dx 0.10.0/0.11.0 to 0.13.0-beta.

Let me know if it works… it’s late and I might of missed something out :P.



I know this was posted a long time ago but the download in the final post seems to be broken. The file downloads but it is corrupted :frowning:


How to implement javascript project ?


how to add if it developed in ios?


Since cocos2d-x v3.8 you can use:


However iOS does not let you control the period of vibration, I believe the duration will be fixed at 0.4 seconds.