Tuesday, October 29, 2013

Completed Happy Place App


  I finally completed the Happy Place app. It's a very simple app that I wanted to create to learn how to use ViewSwitcher and playing sounds. I like how you can use ViewSwitcher to fade in and out.
The basic operation is
1: Display an image ( sad ).
2: When Image is clicked , switch image (happy).
3:  Play short sound file ( happyplace.wav).
4: When sound is complete switch image back to first image (sad).

The only problem I have now is that the sound is not very loud. I might have to resample the sound at a higher amplitude.
Code is listed below. Please feel free to comment with suggestions.

activity_main.xml
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ViewSwitcher1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/Viewsad"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/sad"
        android:clickable="true"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/Viewhappy"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/happy"
        android:clickable="true"
        android:orientation="vertical" >
    </LinearLayout>


</ViewSwitcher>

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity implements OnClickListener {

private static ViewSwitcher switcher;// needed for control of ViewSwitcher
private static View sad;// needed for onClick
private static View isSad;// needed for sadPictureReturn()

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// setup switcher view
switcher = (ViewSwitcher) findViewById(R.id.ViewSwitcher1);
switcher.setInAnimation(this, android.R.anim.fade_in);
switcher.setOutAnimation(this, android.R.anim.fade_out);
sad = (View) findViewById(R.id.Viewsad);
sad.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public void onClick(View arg0) {
// called when sad view is clicked
// switch to happy face and play sound
if (arg0.getId() == R.id.Viewsad) {
switcher.showNext();
MyMediaPlayer.mediaPlaySound(getBaseContext());

// else switch to sad face
} else {
switcher.showPrevious();
}

}

public static void sadPictureReturn() {
// When sound is finished return to sad view
// check if next view is sad. if it is then switch
// if not then do nothing because it is already on sad view
isSad = switcher.getNextView();
if (isSad == sad) {
switcher.showPrevious();
}
}

}

MyMediaPlayer.java
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

public class MyMediaPlayer {
// used Media player for sound instead of SoundPool for OnCompletion Listener
private static MediaPlayer mediaPlayer;

public static void mediaPlaySound(Context context) {
mediaPlayer = MediaPlayer.create(context, R.raw.happyplace);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
MainActivity.sadPictureReturn();
mediaPlayer.release();// finished with sound
}
});
}

}

10,000 hour to Mastery.
10,000 - 506.5 = 9493.5 hours left