Android Audio: Play a WAV file on an AudioTrack

September 14th, 2010

If you’ve managed to hack around the various issues that AudioTrack has, then you are probably enjoying its benefits, such as low latency (in the STATIC mode), ability to generate audio on the fly (in the STREAM mode) and the wonderful ability to access and modify raw sound data before you play it.

However, the problem now is getting that data from a source. Many applications that need to use AudioTrack do not generate PCM audio from scratch (an example of an app that does that would be Ethereal Dialpad and other similar apps). You will probably encounter the need to load and use existing audio samples from file sources such as WAV or MP3 files.

Don’t expect to be able to use MediaPlayer facilities to decode raw audio from WAVs and MP3s. Although MediaPlayer can play those files pretty well, its logic is almost entirely contained in the native layer and there is no option for us to plug in and use the decoders for our own purposes. Thus, we have to decode PCM from audio files manually.

In this article I will cover WAV files and in the next one we will get advanced and read audio from MP3s.

Read the rest of this entry »

mindTheRobot.activate()

September 9th, 2010

Hey everyone.

Just wanted to let you know that summer days are over and I’m going to pay much more attention to MTR starting from the next week.

My goal is 2-3 articles per week so stay tuned!

Android Audio: Problems, Hidden Limitations and OpenSL ES

August 9th, 2010

Thomas Edison and one of his early phonographs

Lately I’ve been digging into Android audio APIs. Earlier I wrote an introductory article that describes the three available APIs for WiseAndroid. Now this article assumes you are familiar with AudioTrack, SoundPool and MediaPlayer at the basic level.

What I want to present in this post is my experience with the existing audio APIs on Android, including the issues and problems I personally faced. I will also shortly cover OpenSL ES, the standard that is expected to be supported in one of the upcoming Android releases.

Read the rest of this entry »

Android UI: Vintage Thermometer – Code Update

August 4th, 2010

After I had posted the original article about making a vintage Android thermometer, it became popular very quickly and I was really surprised how useful some people found it. The code I wrote for that article was meant as an example, a draft of what you could really do using the techniques described in the post.

Freddy Martens from ATS Tech Lab took that code, improved it and modified it for his needs while creating several industrial automation components based on the improved code of Vintage Thermometer. You can have a look at them here. I recommend reading other articles on their blog as well and keeping an eye on it.

Read the rest of this entry »

Android UI: Making an Analog Rotary Knob

July 28th, 2010

For some reason, I really enjoy recreating analog components on Android. Today I’d like to share my experience of re-creating a knob that looks like this:

As you can guess, this type of knob does not have min/max bounds and can be freely rotated as much as you want. This type of control is often used for browsing through a list of options that can be wrapped around. Also, an important property is that this knob does not rotate smoothly but switches from one nick to another. You can imagine that it also clicks when you rotate it – and our Android replica will also make that sound!

Here’s how the Android version looks:

I’ve included the source into this article. However, please be warned it is not completed and you will need to fix or finish few things before you can use this code in your own app.

And now, let’s get down to work!

Read the rest of this entry »

Android Tricks: Multitouch AND 1.5 Support, Same App

July 20th, 2010

I don’t know about you but I have an emotional attachment to old platforms and APIs. They are often sweet and nice in their primordial simplicity, even with their well-understood limitations. Even when they become obsolete, we developers still remember and miss them.

In case of Android, version 1.5 / API level 3 is technically obsolete but is far from being extinct from the device market. In fact, as of the day I write this article, it is one of the most widely installed Android versions on the market. (And if you add the not-so-different 1.6, they take almost half of the market together.) While subsequent versions brought us a lot of goodies in various areas of the platform API, 80% or more of all functionality our apps need today is present in 1.5.

Sure, there are cases when your app just isn’t meant to run on 1.x. For example, its core functionality is based on the APIs that are absent from API level 3 (e.g. you’re making a live wallpaper), or when you don’t want to support old devices for performance reasons, or due to a marketing decision. However, in many cases what makes developers raise the minSdkVersion bar is the need to use some of the 1.6+ APIs, such as the new contact management facilities, signal strength detection or better animation support.

The good news I’m trying to bring in this article – to those who aren’t yet familiar with this trick – is that you can actually use the new API yet keep minSdkVersion at 3. This trick might not work in all cases, and might look like a hack, but it will work for many situations like this – and who knows, might allow your app to reach out to its grateful, ready-to-pay customers who still use 1.5.

Read the rest of this entry »

Strictly For My Readers!

July 12th, 2010

Hey everyone!

I decided to write this post to say thanks to everyone who’s been reading my blog so far. I started MTR around a month ago, and it has been visited almost 10,000 times since then. What is very satisfying to me is that many people subscribe to MTR and watch for new articles. This means that you liked the content and found it at least somewhat useful. Thank you so much!

I am really motivated to continue writing articles for MTR. We will cover various stuff, such as loading OpenGL meshes from 3DS Max, porting native Unix libraries to Android, analyzing and modifying audio on the fly and so on and so on. Be sure to stay tuned.

Starting from this month, you can also find articles written by yours truly on WiseAndroid – along with lots of great articles written by other authors.

Right now, I would like to use the rest of this post to recommend to you some Android goodies that I learnt about from some of you guys – some of the readers of this blog who got in touch with me.

Read the rest of this entry »

Android Beginners: NDK Setup Step by Step

July 6th, 2010

Most Android developers start their learning from pure Java, Android SDK based apps. While everyone is aware that there is the NDK (the Native Development Kit), they don’t face the need to use it. Since NDK is distributed separately from the SDK, including documentations and samples, you are not likely to get familiar with NDK before you actually try it as a solution to one of your development challenges.

Because of that, many people think of NDK as of “black magic” of Android development. Many developers who are not familiar with NDK think it is 1) complex to understand and use, and at the same time a lot of developers will think it is a sort of a 2) silver bullet that can solve any problem that can’t be solved with SDK.

Well, both opinions are rather wrong, as I hope to show further in this post. Although it does have a maintenance cost and does add technical complexity to your project, NDK is not difficult to install or use in your project. And, while there are cases where NDK will be really helpful for your app, NDK has a rather limited API that’s mostly focused on several performance-critical areas, such as:

  • OpenGL, including support for some newer versions that the (Java) SDK supports
  • Math (some, but not all, calculation-intensive algorithms might benefit from being done on the native layer)
  • 2D graphics – pixelbuffer support (only starting with 2.2)
  • libc – it’s there for compatibility and perhaps to allow you to port existing native code

In this tutorial we will take our basic Android development environment that we created in one of the previous articles and add NDK support to it. We will also create a basic skeleton project that uses NDK that you can use as the foundation for your NDK-powered apps.

Read the rest of this entry »

Android Beatz: Making a Drum Machine App

July 5th, 2010

Hello again! I’m back from a wonderful vacation and ready to share some more experience of Android programming. Today I’m presenting a simple drum machine app that I made for fun. If you saw the video above, then you probably got the idea how a drum machine works. The design was inspired by one of the greatest drum machines ever – the AKAI MPC. You can have a look at how the original device looked like for example here. Our app is, of course, greatly simplified compared to AKAI MPC, but you can have some fun with it too.

Here’s a screenshot for a better idea of how the UI looks (clickable):

Some interesting features of this app:

  1. The UI is done using bitmaps and custom controls to imitate the original device
  2. The latency is low enough not to be annoying (at least on my Droid)
  3. You can press multiple buttons at once which is very good for long samples such as the bass drum
  4. The LCD screen control uses a 9-patch bitmap background, and switches between flashing, static and ticker text
  5. The LCD screen uses a custom TrueType font that is included within the app

Now let’s see how the most interesting features of this app were implemented (as usual, the source is available at the bottom of the article).

Read the rest of this entry »

Android UI: Making a Live Wallpaper (Fire Simulation)

June 13th, 2010

In the previous article we used 2D graphics via the Android Canvas to draw a custom UI widget. The Canvas approach is very useful when you need to draw relatively static graphics or when there is not too much complex animation. That is the typical case for UI widgets.

In this article we will develop a live wallpaper that displays a fire simulation that you might remember from the good old days of demo scene and DOS. Since we need very fast graphics, we will use OpenGL as the rendering engine. Although OpenGL is typically used for 3D graphics, it can also be used for hardware-accelerated 2D graphics, such as in this case.

Here’s our plan:

  1. Understand and implement the fire effect
  2. Enable OpenGL to render the fire on the screen
  3. Convert our simulation into a live wallpaper

The source code is available at the bottom of the article (but read on to learn some of its limitations).

Note: The part related to live wallpapers will only work on Android 2.1 or higher.

Read the rest of this entry »