Wednesday, October 30, 2013

Pastebin test

After my last post with all the code in the post I realized that it might be time for a Pastebin account for easier viewing of code. Here is a test of posting the exact same code as before.
 MyMediaPlayer.java
 MainActivity.java
 main_activity.xml

It occurs to me that I really should pick better names and more descriptive.

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

Wednesday, October 23, 2013

500 hours

 After about a year and eight months I have 500 hours of practice in Android programming. I have slowly learned  and relearned a lot. There have been wonderful moments and many frustrating ones. I have created Apps but none of which I felt were good enough to upload to Google play, even as a free app. The good news is that I haven't given up and I continue.
   I am putting the finishing touch on a silly little app that switches images and plays a sound. In this case it goes from a sad face to a happy face and plays a wav file named happyplace . It was from a list of exercise objectives I made when I first started but I put off while I did others. The objective was to have the app display one image and when touched it would switched the image while playing a sound. Then afterwards switch back. There were a number of ways I could do this.
 One way would be to just set image in view and make it clickable which would then change the image while playing sound, all controlled by one main activity class.( which I did)
 A second way would be split up the main view activity and the sound. Putting the sound code in a separate class ( which I also did).
 The first way is the easiest but not very OOPs compliant in which you try to isolate code in to it's own pieces.The second way gave me a bit of trouble since SoundPool requires a Context and I was not sure how to give it one. I first tried by making the Class extend Activity but since the class did not actually have a UI that was not correct. I finally settled on passing getBaseContext when I called MyPlayer.
  One cool thing I learned about was ViewSwitcher. It has a nice animation that can fade in and out when changing Layout. You set up 2 layouts on one xml and it will switch between them.
  A problem I am now working on is having it switch back after the sound has played. As far as I know there is no onCompleteListener for SoundPool. So I tried a timer but I am having a bit of trouble in that it either goes from the first image to the second and back before you can see it or if I set it up another way it waits till the time is up before switching from the first image to the second

10,000 hour to Mastery.
10,000 - 502.5 = 9497.5 hours left
( I know it's going very slow but other obligations prevent me from devoting more time to studying)

Tuesday, October 1, 2013

The importance of backing up

  Last week my hard drive crashed and with it all my work on Android and the App I was working on. Silly me forgot to back it up. After spending hours installing a new hard drive and all the software as well as updates, I NOW schedule backups so I don't forget. The good news is that I think I  have a better start on the App I was making. A fresher looking app. A better idea of how to make it.

10,000 hour to Mastery.
10,000 - 486 = 9514 hours left