Android MediaPlayer and Garbage Collection

So here is the scenario, you are making the most amazing application ever, and you want a notification sound to verify something great has gone through. Well, you implement the MediaPlayer and maybe the AudioLoader to play your sounds. Regardless of it being Asynchronous, which it should be. If not, shame on you because it is supported by android utilizing a simple thread and prepareAsync. But, I digress, regardless of that fact, when you go to use this feature, sometimes the sound plays and sometimes it just gets butchered and cuts off immediately when you change views or the function ended. Why? Garbage Collection. You didn’t make the MediaPlayer a global object, therefore when the function ends, garbage collection collects all of the non-referenced objects or global objects to trash them. Lo and behold, MediaPlayer gets captured. Working Solution:

... class
  private MediaPlayer mp;

  public void playSound() {
    mp = new MediaPlayer();
    // ... do your prepare listeners and completion listeners
    mp.prepareAsync();
  }
...

Posted 5/23/12. 0 notes.

The Rulist, Beautiful Javascript Validation Syntax

Lately I have been coding on Node.js servers a lot, which utilize pure JavaScript or CoffeeScript, if you so choose. During this time, I’ve had to validate tons of data or just create quick little tests to make sure something is correct with a few rules, now doing this with if statements is long and drawn out, and can make your code unruly really fast. I developed a easier more concise syntax method of doing this after a moment of pure clarity in the shower:
  return (
      (!(username += '') || username == '')  ? { error: "No Username Given.", field: 'name' }
    : (!(username += '') || password == '')  ? { error: "No Password Given.", field: 'pass' }
    : (username.length < 3)                  ? { error: "Username is less than 3 Characters.", field: 'name' }
    : (password.length < 4)                  ? { error: "Password is less than 4 Characters.", field: 'pass' }
    : (!/^([a-z0-9_-]+)$/i.test(username))   ? { error: "Username contains invalid characters.", field: 'name' }
    : false
  );

This example is attacking username and password with a rule list or Rulist as I call it. Whenever it hits true it stops, otherwise we continue down the list.

The beauty of this is not only how concise it is but the legibility, and how flexible it can be. I’ve garnered a lot of responses to this style as not being legible but the vast majority of those people had just began javascript or have not worked much with ternary operators.

So by working with this style, you will not only gain readability, but more knowledge about how the language works and some of the optimizations you can make as well.

I just like it because it’s concise, and I can easily read it. To each their own right?

Breaking It Down

In this section we are going to look more into the inner-workings of how The Rulist syntax works; Getting a better feel for how to accurately do things and how it works.

Basic Validation

  (!username || username === '') ? { error: "No Username Given.", field: 'name' }  // Doesn't Exist? Return this
  : false // Otherwise, nothing to return; False is good. It means it passed validation.

Complex Validation

Lets say you want to do some regular expression testing, matching and such. It’s really easy and not only can you test, but you can also set variables inside of if statements.

  (!/^([a-z0-9-_]+)$/i.test(username))  ? { error: "Username contains invalid characters.", field: 'name' } // Test RegExp. Return on failure
  : false // It passed!

Nesting Validation

Nesting isn’t that hard and it’s pretty simple, and manages to come out to be really concise too.

  (!username)   ?
  (
    (!password) ? { error: "Missing Username and Password.", field: ['name', 'pass'] }
    :             { error: "Missing Username", field: 'name' }
  )
  : false 

There is more, but I think that is good enough for now. I’ll let you play around with it yourself!

Posted 5/18/12. 1 note.