Android: recording a video of your application

As I am releasing a new application any day now (Kludd) I was in need to record some gameplay from the app in order to do some kind of video promo. I had no idea how to record from a telephone (I knew how to take screenshots on my Samsung Galaxy S4 which was by holding down the on/off-button and the home button simultaneously) so I googled it.
I found that you could do it using your Android SDK. These are the steps in short:

* Connect your android phone to your computer
* In a command prompt, step into your android sdk directory and then platform-tools directory
* Enter the following command: adb shell screenrecord /sdcard/FILENAME.mp4 (cancel/end the recording by pressing CTRL + C)

(don't mind the errors, the recording still works!)
(don’t mind the errors, the recording still works!)

 

After that either go in on the phone via your computer explorer and enter /Phone/ and you’ll find the video there or find it using whatever exploration app you want on the phone.

PhoneGap: A very basic android HTML5 app

I decided today to update my version of PhoneGap and found that instead of the old downloadable zip with JARs and stuff there was only an instruction to update it via the Node.js command line interface. Well, that works well for me too, but for the sake of one of my colleagues I thought I’d better write a small tutorial on how to do this.

 

Install/Update PhoneGap

Begin by installing node.js if you haven’t (if you have problems at this stage I refer to some article I found on the googely net).
After you have installed Node.js just follow the instructions found on the PhoneGap web site: http://phonegap.com/install/ (run the commands in your node.js command prompt).

Create your first app

Well, I looked around for a good tutorial and I found this one on the PhoneGap web. It worked somewhat well for me. I just got some reeeeally weird error messages such as:

PhoneGap 3.0 Android build Command failed to execute : ant jar

This error is easiest fixed by installing ant and properly setting your ANT_HOME environment variable. Remember that you may have to restart your prompt in order to get everything updated and ready.
After that error I got this one:

Error: An error occurred while listing Android targets

This was even more annoying since it was so generic. The error did contain a stacktrace however and I followed it back to its source (as the scripts are written in non-scrambled javascript). The problem was apparently that the script was trying to execute an android command (android list targets as it were) and it failed. This was fixed by installing the Android SDK properly and setting the ANDROID_HOME environmental variable correctly :).

After all that, I was ready to roll so I created my first example project according to the tutorial:

Create the project: cordova create hello com.example.hello HelloWorld

CommandPrompt1

This creates a dir called hello with your project in it. Enter that dir and run the following command:
Adding android to your app: cordova platform add android

You should now see something like this in your prompt:
CommandPrompt2

You now have a PhoneGap project for android!

Building and running

To build: cordova build
To run the emulator with your project: cordova emulate android (emulate) OR cordova run android (on a real device)

You might encounter the following error:
Error executing “adb devices”: ‘adb’ is not recognized as an internal or external command…
I solved this problem by adding the android-sdk/platform-tools/ to the PATH variable.

Edit your project

Add whatever HTML you want in the www/ directory!
If you want to tamper with the AndroidManifest enter the platforms/android/ directory!

Import it into Eclipse

To import the project into Eclipse open up the IDE and go to File => New => Other… =>Android Project from Existing Source and select the base folder (“hello” in this case) of your phonegap project. You might have a problem with errors such as “KITKAT cannot be resolved or is not a field” which most likely means you haven’t installed the proper SDK API. It happened to me and I hadn’t installed the Android 19 SDK so I just went to Window => SDK Manager and installed it. I then right-clicked on the faulty project (most likely the CordovaLib project) and chose Properties. I then opened the Android tab and selected API Level 19 instead of 18 which was selected.

Right, that was a very short basic tutorial and you should now have some basic knowledge of how to setup a PhoneGap project!

No thanks, I’ll do this only through eclipse instead. Where are the .jar files!?

Allright, the above way is a bit of a hassle and I prefer to use Eclipse my self too. If you are going to use eclipse instead you need the cordova-3.3.0.jar file instead. This doesn’t exist yet so good luck finding it. To acquire one, do the following:

CommandPrompt3

Now you’ll have your .jar file in that directory. Just import it into your eclipse project

PhoneGap: Attempt to call getDuration without a valid mediaplayer

I use phone-gap to handle the gap between HTML 5 and Android and I’ve been very pleased with the framework thus far. Today, however, I had a very annoying issue with the Media-object found in PhoneGap. I got the following error:

10-27 20:02:14.039: E/MediaPlayer(16984): error (-19, 0)
10-27 20:02:14.039: V/MediaPlayer(16984): callback application
10-27 20:02:14.039: V/MediaPlayer(16984): back from callback
10-27 20:02:14.039: V/MediaPlayer(16984): getDuration_l
10-27 20:02:14.039: E/MediaPlayer(16984): Attempt to call getDuration without a valid mediaplayer
10-27 20:02:14.039: V/MediaPlayer(16984): message received msg=100, ext1=-38, ext2=0
10-27 20:02:14.039: E/MediaPlayer(16984): error (-38, 0)

The Attempt to call getDuration without a valid mediaplayer part of the error seemed to be the most usable and I googled it. It turned out that android can only run a finite amount of sound instances. From the PhoneGap documentation:

Function media.release is a synchronous function that releases the underlying operating systems audio resources. This function is particularly important for Android as there are a finite amount of OpenCore instances for media playback. Developers should call the ‘release’ function when they no longer need the Media resource.

So, I added release() to my code, like this:

var change = new Media("change.wav");
change.setVolume = GlobalSettings.EffectVolume;
change.play();
change.release();

And, instead I got the following error in my catalog:

10-27 20:10:26.049: D/AudioPlayer(21033): renaming /storage/emulated/0/tmprecording.3gp to change.wav
10-27 20:10:26.049: E/AudioPlayer(21033): FAILED renaming /storage/emulated/0/tmprecording.3gp to change.wav

The problem seemed to be that the file wasn’t finished playing (obviously) so it crashed. I then tested to place the release after the sound had finished playing:

var change = new Media("/android_asset/www/sound/effects/Change.wav", function()
{
	this.release();
});
change.setVolume = GlobalSettings.EffectVolume;
change.play();

This has worked well and has caused no crashes. The second, optional, parameter of the Media object takes a callback that will be called when the sound has finished playing.

JAVA: JAVA_HOME should point to a JDK not a JRE

Today when I was updating our JIRA instance I got the following error when I tried to start it:

The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

This made no sense since I had Java installed so I did the right thing and checked my JAVA_HOME environment variable:


C:\>echo %JAVA_HOME%
C:\Program Files (x86)\Java\jdk1.6.0_25

So… the JAVA_HOME variable was pointing in the right direction. The problem turned out to be that the path to the JDK folder contained spaces (in Program Files (x86)). So I went to C:\ and ran the command dir/x to see what the sort name for Program Files (x86) was:


C:\>dir/x
Volume in drive C is OS
Volume Serial Number is [none of you business]

Directory of C:\

...
21/08/2013 19:27 DIR eclipse
10/08/2012 17:09 DIR inetpub
22/03/2013 20:21 DIR NVIDIA
26/07/2012 09:33 DIR PerfLogs
23/07/2013 17:44 DIR PROGRA~1 Program Files
23/08/2013 09:14 DIR PROGRA~2 Program Files (x86)
08/07/2013 18:04 DIR Projects
14/07/2013 09:27 DIR Users
18/08/2013 17:38 DIR Windows
3 File(s) 14,643 bytes
12 Dir(s) 1,797,314,781,184 bytes free

The short hand version of Program Files (x86) was, as we can see, PROGRA~2. So I changed the environment variable to C:\PROGRA~2\Java\jdk1.6.0_25 by right clicking My Computer and selecting properties, then Advanced System Settings, Advanced tab, Environment Variables and locating it in the list (I’m using Windows 8 btw). That seems to have solved it.

Eclipse: annoying 32/64-bit installation issues

I just installed Eclipse on my new computer that’s running windows 8 and I ran into some problems. First I got the following error message:

---------------------------
Eclipse
---------------------------
A Java Runtime Environment (JRE) or Java Development Kit (JDK)
must be available in order to run Eclipse. No Java virtual machine
was found after searching the following locations:
C:\eclipse\jre\bin\javaw.exe
javaw.exe in your current PATH
---------------------------
OK
---------------------------

This was due to the fact that I had actually forgotten to install java which was needed 🙂
I installed java and changed the eclipse ini-file to include the path to the JRE:

-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20130521-0416
-product
org.eclipse.epp.package.standard.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
-vm
C:\Program Files(x86)\Java\jre7\bin\javaw.exe
--launcher.appendVmargs
-vmargs
-Dosgi.requiredJavaVersion=1.6
-Xms40m
-Xmx512m

I then got the following error message:

---------------------------
Eclipse
---------------------------
Java was started but returned exit code=13
C:\Program Files (x86)\Java\jre7\bin\javaw.exe
-Dosgi.requiredJavaVersion=1.6
-Xms40m
-Xmx512m
-XX:MaxPermSize=256m
-jar C:\eclipse\\plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar
-os win32
-ws win32
-arch x86_64
-showsplash C:\eclipse\\plugins\org.eclipse.platform_4.3.0.v20130605-2000\splash.bmp
-launcher C:\eclipse\eclipse.exe
-name Eclipse
--launcher.library C:\eclipse\\plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20130521-0416\eclipse_1503.dll
-startup C:\eclipse\\plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar
--launcher.appendVmargs
-exitdata 1868_44
-product org.eclipse.epp.package.standard.product
-vm C:\Program Files (x86)\Java\jre7\bin\javaw.exe
-vmargs
-Dosgi.requiredJavaVersion=1.6
-Xms40m
-Xmx512m
-XX:MaxPermSize=256m
-jar C:\eclipse\\plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar
---------------------------
OK
---------------------------

I searched the internet and I found out that I had, for some inexplicable reason, installed a 32-bit version of Java on my 64-bit system. Why? Well, it turned out that my browser was a 32-bit version of Chrome and so when I entered java.com and downloaded the installation package they handed me a 32-bit package which I installed. The fact that the path to the JRE included “Program Files(x86)” should probably have been a clue, but no.
I read up and it turned out I had to install both a 32-bit version and a 64-bit version and feed eclipse, which I downloaded as a 64-bit version, the path to the 64-bit JRE installation.

Then it worked fine 🙂

Eclipse: Adding prefix rule for fields

As I’m every now and then trying out new things in Eclipse I found out that’s quite annoying to use any kind of convention that includes prefixing your fields. In C# I always prefix my fields with “m_” so that age becomes m_Age and name becomes m_Name.

The problem with that is that when you use the getter/setter auto generator in Eclipse (right-click on a field and choose Source => Generate getters and setters…) then you end up with getters that are named getm_Age and getm_Name instead of getAge and getName.

Luckily it turns out that you can set your own prefixes in Eclipse by going to Window => Preferences => Java => Code Style and selecting the variable type that you want to edit and add your prefix.