teaching machines

Homework2-wirthaw

October 19, 2011 by . Filed under cs491 mobile, fall 2011, postmortems.

My app was a rather fun(ny) app to make, it is a stand up comedy clip app. The general goal of the app is to provide users with a list of comedy clips they can click on and listen to. Ideally this app would ultimately be able to download new clips for some online database however for now its just a few local clips I preloaded into a database.

 

 

The main screen is simply a welcome screen with some random applause for effect, just a nice title for the app but doesn’t really serve a purpose. After you “tap to start” the app takes you to a list populated by database full of “clips”. A clip contains pointers to an audio file, picture, and strings for the clip title and artist name. From here you can either shake the mobile device to select a random clip or select a clip on the list. The next view/activity is a simple layout containing just a picture of the artist, and a play button. It plays the clip all the way through and then finishes. You can press the back button to finish() early or press the play/pause button to play/pause the clip. I didn’t include a next or a time slider under the assumption anyone who clicked on the clip wants to hear the whole thing.

As I mentioned you can shake the phone to select a random clip. Now unfortunately there is no pre-programmed “shake” sensor so this had to be programmed using the accelerometer and some neat code off of the android forum stackoverflow  here is the full site and then some of the code http://stackoverflow.com/questions/5271448/how-to-detect-shake-event-with-android.

@Override
public void onSensorChanged(int sensor, float[] values) {
if (!shakeDetected) {
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
long curTime = System.currentTimeMillis();
// only allow one update every 100ms.
if ((curTime – lastUpdate) > 100) {
long diffTime = (curTime – lastUpdate);
lastUpdate = curTime;

x = values[SensorManager.DATA_X];
y = values[SensorManager.DATA_Y];
z = values[SensorManager.DATA_Z];

float speed = Math.abs(x + y + z – last_x – last_y – last_z) / diffTime * 10000;

if (speed > SHAKE_THRESHOLD) {
// find random number between 0 and 4
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(5);
//Toast.makeText(this, “” + randomInt, Toast.LENGTH_SHORT).show();
// tell activity a shake was detected then start new
// activity but wait for result.
shakeDetected = true;
Intent intent = new Intent(MainActivity.this,ListenClipActivity.class);
intent.putExtra(“currentClipID”, adapter.getItem(randomInt).getID());
startActivityForResult(intent, REQUEST_CLIP_FINISHED);
}

last_x = x;
last_y = y;
last_z = z;

}
}

Anyway, basically to sum this up there is a little Math.abs function in there that determines how fast the sensors moved/changed. If the movement was faster than the “SHAKE_THRESHHOLD” (just some number in a mid range speed to determine a moderate shake-1000 here) than i create a new intent with a random clip id. There are a few booleans in there to make sure I only fork off one new intent and not 800 new intents (happened to me).

Making the play button was easy. Use a OnClickListener on the playButton picture. Then if the player was playing call player.pause() and if it was  paused call player.start(). The tricky part was switching the image from a play to a pause this was done using .setImageResource(r.drawable.”newimage”). That function takes an int which the r.drawable.image actually references.

Random quick side note is that i was a little disappointed that android doesn’t have a preprogrammed audio controller for audio clips. That seems like it would be pretty standard.

Also this is a stand up comedy app so some of the clips do contain vulgar language, still hilarious though.