How to add Vibration to android game

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 Cocos2dxActivity.java and Cocos2dxsound.java are generated in your project directory everytime you run the create-android-project.bat file. You must place Cocos2dxActivity.java and Cocos2dxsound.java and into:

“2dx\lib
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:

CocosDenshion::SimpleAudioEngine::sharedEngine()->vibrate(time);

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:

CocosDenshion::SimpleAudioEngine::sharedEngine()>vibrateWithPattern;
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:

CocosDenshion::SimpleAudioEngine::sharedEngine()->cancelVibrate();

The methods were created with intention of keeping the same structure of the vibrate methods from the android sdk: http://developer.android.com/reference/android/os/Vibrator.html.


Vibrate.zip (10.9 KB)


Vibrate.zip (12.3 KB)


Vibrate.zip (12.3 KB)

1 Like

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.

‘Android.mk’ has been changed to add the ‘AndroidEffectsEngine.cpp’ to the library libcocosdenshion when compiling

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

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

I have enclosed 7 files:

Android.mk
AndroidEffectsEngine.h
SimpleAudioEngine.h
AndroidEffectsEngine.cpp
SimpleAudioEngine.cpp
Cocos2dxActivity.java
Cocos2dxSound.java
Cocos2dxAndroidEffects.java

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

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

‘Cocos2dxActivity.java’ and ‘Cocos2dxSound.java’ and ‘Cocos2dxAndroidEffects.java’ goes into the “$cocos2dxRootdirectory\tests\test.android\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.

Enjoy

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:

cocos2d::Device::vibrate(duration);

https://github.com/cocos2d/cocos2d-x/blob/v3/cocos/platform/CCDevice.h

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