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.

Android error: Error generating final archive: java.io.FileNotFoundException: C:\[PATH]\resources.ap_ does not exist TestingAndroid

I got this annoying error now when trying to run my application:


Error generating final archive: java.io.FileNotFoundException: C:\Users\maffelu\workspace\TestingAndroid\bin\resources.ap_ does not exist

There were several solutions available on google:

  • Do a clean (didn’t work for me)
  • Restart eclipse (the windows way didn’t work either)
  • Create a new resources.ap_ file in the directory specified in the error message (this did work for me, wohoo!)
  • Reinstall the Android SDK (thankfully it didn’t go this far…)

Feel free to try all solutions until it gets fixed 😛

Android error: Error generating final archive: Debug Certificate expired on [xxx]

I got this error when I tried to start my android application:


Error generating final archive: Debug Certificate expired on 2012-01-14 21:26 TestingAndroid Unknown Android Packaging Problem

I hadn’t been androiding for a while and I’d never seen this before. Luckily google could help me on this one, but I’ll put the solution here anyway:

  1. Go to %USERPROFILE%/.android
  2. Delete the debug.keystore file there
  3. Clean your project and rebuild
  4. Done!

The error should be gone now!