Posts Tagged 'splash'

Splash Screen Demo With Sound

I did a little experiment to see if I could add sound to my splash screen demo. I never had done anything with sound in Android but had wanted to try it out for quite some time. It turns out that it was very easy to do.

The demo app looks exactly like the previous Splash Screen demo. The only difference is that it nows plays sounds.  It plays two distinct sounds. The first is for the splash image. The second is for the main screen. The second sound plays when the main activity starts and at each time you reorient your Android device.

Here’s what I did:

1. I moved two mp3 and wav files into the res/raw folder.

2. In my activity’s onCreate method, I added code to create a new MediaPlayer objects. Then I started it playing. The code looks like this:

mp1 = new MediaPlayer (this, R.raw.laser_01);
mp1.start ();

3. In my activity’s onDestroy method, I added code to stop the media player and release any resources it had.

if (mp1 != null) {
   if (mp1.isPlaying ()) mp1.stop ();
   mp1.release ();
   mp1 = null;
}

If you are interested in the source code for the demo, here is a link to a download page for it: download Splash with Sounds.

In researching this, I came up with some questions that I want to follow up on.

  • When should you use SoundPool and when should you use MediaPlayer?
    From what I can tell so far, it seems like SoundPool is best for adding sound effects for apps. The MediaPlayer is better for longer sounds.
  • What’s the best way to maintain the state of a MediaPlayer?
    I had some trouble when I created the media player in the onCreate method and starting it onResume. I was getting an exception about the media player being in an illegal state. So I don’t yet understand the MediaPlayer methods fully.
  • How long is too long for a sound file being played in the main (UI) thread?
    The example I built plays the sound on the main thread. If the duration of the sound were longer, it would be good to move it into a background task.

References

Media Playback note – general information on the Android Developers website about the MediaPlayer

Question about sound on Stack Overflow

Android Sounds Tutorial by Lars Vogel - Example of a SoundPool. This seems like the way to go to have a set of sounds you use over and over again.

Splash Screen Demo App for Android

Recently I wrote a blog article on splash screens for Android apps. I didn’t take the time then to build a simple demo app with source code so I’m doing so now.

There is not really much to my Splash Demo app. I took the basic Hello world app and added a splash screen. The splash screen stays visible for about 5 seconds and then switches to the Hello activity. The app looks like the following:

 

Source Code

Download the source code for the Eclipse project from this page at wglxy.com:

http://www.wglxy.com/android-tutorials/splash-screen-demo-app-for-android

If you find that the app does not build in Eclipse, be sure to do a clean build by using the “Clean” item on the Project menu. The project was built with Android 2.2.

Building the Demo

Here are steps I took to build this demo app.

  • I started with the default Android project that displays “hello”.
  • I added a LauncherActivity and changed the Android manifest file to start that activity rather than the MainActivity.
  • I built an image, 512 x 512, to use as the splash image.
  • I added a layout xml file for LauncherActivity that displays the image on a black background.
  • I changed the onCreate method of the LauncherActivity so it displays its view for 5 seconds and then starts the MainActivity.
  • I added an AsyncTask class as an internal class of LauncherActivity in order to have a background task triggering the transition to the MainActivity.

The last two steps require a bit more explanation.

The onCreate method of LauncherActivity is the place where the transition to the main activity happens. It does two things. It sets up to handle clicks on the image view showing the splash image. That provides a user with a quick way to skip the splash screen. The second thing onCreate does is start a second task. The GoHomeAfterDelay class is a subclass of AsyncTask. It runs in the background, or, in this case, it sleeps in the background. When it awakes, the task finishes and triggers a call to start the main activity. Note also that I put the actual time of delay, 5000 milliseconds, in a values.xml resource file as the integer named splash_screen_time.

protected void onCreate(Bundle savedInstanceState)
{
 super.onCreate(savedInstanceState);
 setContentView(R.layout.launcher);

 // Set up to go to the home screen if a user touches the image.
 ImageView i1 = (ImageView) findViewById (R.id.splash_image);
 if (i1 != null) i1.setOnClickListener(this);

 // Set up to go to the home screen after a short delay.
 int delay = getResources ().getInteger (R.integer.splash_screen_time);
 mDelayTask = new GoHomeAfterDelay (this);
 mDelayTask.execute (delay);
}

The GoHomeAfterDelay class is a subclass of AsyncTask. Its definition is at the bottom of the LauncherActivity.java file. GoHomeAfterDelay’s doInBackground method does nothing more than go to sleep for the time specified. Part of the protocol for any AsyncTask involves executing its onPostExecute method when the task completes. So that’s the spot where we end the launcher activity and start the main activity.

/**
 * Perform the task, which is to wait N milliseconds.
 * Return 1 if the wait completed or 0 if the task was cancelled.
 */
@Override protected Integer doInBackground(Integer... delayValues)
 {
   int delay = delayValues [0];
   try {
     Thread.sleep (delay);
   } catch (InterruptedException ex) {} if (isCancelled ()) return 0;
   else return 1;
 } 
/**
 * Report that the task has finished.
 * This method runs in the UI thread.
 */ 
@Override protected void onPostExecute (Integer moveCount)
 {
  if (! mCancelled) {
     if (mActivity != null) mActivity.goHome (true);
  }
  mActivity.mDelayTask = null;
 }

If you are unfamiliar with AsyncTask and the reason for it, I refer you to two earlier blog notes:  AsyncTask note , Long Computations note.

The place where MainActivity is actually started is in the goHome method. It uses an Intent to start the activity. After that, there is a call to the finish method. That’s what stops the LauncherActivity and keeps it from showing up again when you hit the Back button.
public void goHome (boolean doFinish)
{
  final Intent intent = new Intent(this, MainActivity.class);
  intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
  this.startActivity (intent);
  if (doFinish) finish ();
}

Acknowledgements

As I said earlier, there are several good articles about splash screens already out there. My favorite is the one on “Android Splash Screens Done Right“. Thanks go to Mr. Clifton for sharing this very useful information.

Good Articles About Android Splash Screens

I am getting close to finishing my first Android app. One of the things I still had to do was a splash screen for the app. It didn’t take me long to do, thanks to these two articles:

If you are not sure you need a splash screen for your app or website, you should read the Wikipedia note about the purpose of splash screens. For me, the motivation is the aesthetic one. I don’t have the graphic design skills myself, but if you can find someone to help you, I think an app makes a better impression with a good splash screen.



Follow

Get every new post delivered to your Inbox.

Join 72 other followers