MusicDroid - Audio Player Part III

SDK Version: 


In part 1 and 2 we created a simple media player, but there was no way to do anything except play a selected song. Now we must create some kind of interface to control the music. I am not a great graphical designer so the controls might be a little bland for now, but it demonstrates how to control layouts using RelativeLayouts and ImageViews, as well as animating the image views. It also demonstrates how to create a transparent activity.

Click here to download the full source.

Layout for 4-way controls

For the controls menu that is pictured we will use 4 image views, in 2 relative layouts. Here is controls.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout xmlns:android=""
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent">
  7.         <RelativeLayout xmlns:android=""
  8.             android:layout_width="170dip"
  9.             android:layout_height="170dip"
  10.             android:layout_centerVertical="true"
  11.             android:layout_centerHorizontal="true">
  13.                 <ImageView id="@+id/pause"
  14.                         android:layout_alignParentTop="true"
  15.                         android:layout_centerHorizontal="true"
  16.                         android:layout_width="50dip"
  17.                         android:layout_height="50dip"
  18.                         android:src="@drawable/menupause"; />
  20.                 <ImageView id="@+id/skipb"
  21.                         android:layout_alignParentLeft="true"
  22.                         android:layout_centerVertical="true"
  23.                         android:layout_width="50dip"
  24.                         android:layout_height="50dip"
  25.                         android:src="@drawable/menuskipb"; />
  27.                 <ImageView id="@+id/skipf"
  28.                         android:layout_alignParentRight="true"
  29.                         android:layout_centerVertical="true"
  30.                         android:layout_width="50dip"
  31.                         android:layout_height="50dip"
  32.                         android:src="@drawable/menuskipf"; />
  34.                 <ImageView id="@+id/stop"
  35.                         android:focusable="true"
  36.                         android:layout_alignParentBottom="true"
  37.                         android:layout_centerHorizontal="true"
  38.                         android:layout_width="50dip"
  39.                         android:layout_height="50dip"
  40.                         android:src="@drawable/menustop" />
  42.         </RelativeLayout>
  43. </RelativeLayout>

The first Relative layout fills the full width and height of the screen so that we can center the second relative layout which is 170x170 pixel square which will hold our buttons.

To center a View in the horizontal we use "android:layout_centerHorizontal", and to center in the vertical we use "android:layout_centerVertial". With RelativeLayouts the order in which you list the items does not matter, you place them using these alignment techniques.

So, we create 4 ImageViews here. The first is the pause button which is centered horizontally and aligned at the top vertically. The bottom button is the stop button, which is also centered horizontally, but vertically positioned at the bottom. And of course skip forward and back are both centered vertically, but aligned to the right and left.


Animation xml files are placed in a folder (you will have to create), "res/anim". Animation XML files are very easy to use to translate, scale, rotate, adjust alpha, and create a tint. There are currently some limitations with the animation functionality unfortunately. The 2 big issues right now is that the AnimationListener functionality does not work. This means that there is no way to tell when an animation is finished running. Also, there is a property called "fillAfter" which does not work. This property, when set, will not redraw the object when an animation is finished. For example if you set fillAfter to true, and then rotate an object, when the object is done rotating it should stay at the new angle. Unfortunately, it will always redraw the original object when the animation is finished.

For this example we will do just a very simple scale animation, so that when you hit a control the icon gets large and then scales smaller. Please feel free to mess around with this, remember you can scale, translate, rotate, adjust alpha, and tint objects. If you come up with something really cool let us know in the forum!

Here is our simple scale animation, which we can create in "res/anim" and call selected.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="" android:shareInterpolator="false" android:fillAfter="true">
  4.         <scale
  5.                 android:fromXScale="1.5"
  6.                 android:fromYScale="1.5"
  7.                 android:toXScale="1"
  8.                 android:toYScale="1"
  9.                 android:duration="600"
  10.                 android:pivotX="50%"
  11.                 android:pivotY="50%"
  12.                 android:fillAfter="true"
  13.                 />
  15. </set>

So lets talk about what we are saying here. We are saying that when this animation starts we want to have the image scaled to 1.5 in the X and Y, and when the animation is done we want it to scale back to 1 in the X and Y. The duration states that this animation will take 600 ms. The pivotX and pivotY describe the points at which to scale around. Ie, you could have the left edge stay where it is and have it expand to the right, but this case we just specifiy to scale from the center.

This is a simple animation that works for the controls that we have designed, but lets say after the scale you wanted to do another animation, for example fade the object away. Although that does not make sense for these controls, I thought it would be good information to include. So to create another action that will execute after the scale you can create another tag, alpha in this example, and set the "startOffset" equal to the ammount of time that it took for the first animation to complete (or more or less if you want to have it start sooner or delay). Here is an example of an alpha animation that could be inserted after the scale animation above:

  1. <alpha android:fillAfter="true"
  2.         android:startOffset="600"
  3.         android:fromAlpha="1.0"
  4.         android:toAlpha="0.0"
  5.         android:duration="400" />

I recommend taking a look at the different types of animations in the API demos to see more.

Transparency using Themes and Colors

To make this activity transparent we will use a custom theme. All that we need to do is create a custom theme and set the background color to a color that we must define. This is very easy because colors are 8 digit hexidecimal numbers which include the alpha value. The last 6 digits are the RGB values, much like an HTML color. The first 2 digits is the alpha value, and that is what we must set to get a translucent background.

To create a theme you create a file called "styles.xml" in the "res/values" folder. It is not required to be named styles.xml, but that is the best practice. Here is the styles.xml file we will use:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  4.         <style name="Theme" parent="android:Theme.Dark"></style>
  6.         <style name="Theme.Transparent">
  7.                 <item name="android:windowBackground">@drawable/transparent_background</item>
  8.                 <item name="android:windowNoTitle&quot;>true</item>
  9.         </style>
  10. </resources>

As you can see the theme we created is a child to the standard dark theme, this means that it will inherit its properties. All we need to do is set the background color and hide the window's title. The color referred to here as "@drawable/transparent_background" is defined in colors.xml in the "res/values" folder:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.      <drawable name="transparent_background&quot;>#a0000000</drawable>
  4. </resources>

Notice that there is no color defined in the last 6 digits, we just define the alpha value to be A0 out of FF (160 out of 255 for those that don't automatically convert hex in their heads).

Now that we have this custom theme setup we have to somehow tell our ControlsMenu Activity (which we'll create on the next page) to use this custom theme. That is done in the AndroidManifest.xml file, here is the entry we will add for this new Activity:

  1. <activity class=".ControlsMenu" android:label="@string/app_name"
  2.                 android:theme="@style/Theme.Transparent" />

No we have created all the XML we need, so lets put it to use in the next page and create the ControlsMenu Activity...

The Activity

This is a very simple Activity. It's not very smart, it's not aware of what is being played (yet). It's only role is to display 4 images, and when a user hits a key corresponding to one of them it needs to send a command to the service using the Interface from part 2 and trigger an animation to start.

Because this is a simple Activity we'll show the whole thing and discuss after:

  1. public class ControlsMenu extends Activity {
  3.         private ImageView pauseImage;
  4.         private ImageView skipbImage;
  5.         private ImageView skipfImage;
  6.         private ImageView stopImage;
  7.         private MDSInterface mpInterface;
  9.         @Override
  10.         public void onCreate(Bundle icicle) {
  11.                 super.onCreate(icicle);
  13.                 setContentView(R.layout.controls);
  15.                 pauseImage = (ImageView) findViewById(;
  16.                 skipbImage = (ImageView) findViewById(;
  17.                 skipfImage = (ImageView) findViewById(;
  18.                 stopImage = (ImageView) findViewById(;
  20.                 this.bindService(new Intent(this, MDService.class), null, mConnection,
  21.                                 Context.BIND_AUTO_CREATE);
  22.         }
  24.         @Override
  25.         public boolean onKeyUp(int keyCode, KeyEvent event) {
  26.                 try {
  27.                         switch (keyCode) {
  28.                         case KeyEvent.KEYCODE_DPAD_UP:
  29.                                 handleAnimation(pauseImage);
  30.                                 mpInterface.pause();
  31.                                 break;
  32.                         case KeyEvent.KEYCODE_DPAD_LEFT:
  33.                                 handleAnimation(skipbImage);
  34.                                 mpInterface.skipBack();
  35.                                 break;
  36.                         case KeyEvent.KEYCODE_DPAD_RIGHT:
  37.                                 handleAnimation(skipfImage);
  38.                                 mpInterface.skipForward();
  39.                                 break;
  40.                         case KeyEvent.KEYCODE_DPAD_DOWN:
  41.                                 handleAnimation(stopImage);
  42.                                 mpInterface.stop();
  43.                                 break;
  44.                         }
  46.                 } catch (DeadObjectException e) {
  47.                         Log.e(getString(R.string.app_name), e.getMessage());
  48.                 }
  50.                 return super.onKeyUp(keyCode, event);
  51.         }
  53.         public void handleAnimation(View v) {
  54.                 v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.selected));
  55.         }
  57.         private ServiceConnection mConnection = new ServiceConnection() {
  58.                 public void onServiceConnected(ComponentName className, IBinder service) {
  59.                         mpInterface = MDSInterface.Stub.asInterface((IBinder) service);
  60.                 }
  62.                 public void onServiceDisconnected(ComponentName className) {
  63.                         mpInterface = null;
  64.                 }
  65.         };
  66. }

First in the onCreate() function we initialize all of our private variables for the ImageView objects. Then we attempt to bind to our service we created. Once this binds it will call onServiceConnected down on line 74 which will initialize our interface to the service.

The user input is handled in the onKeyUp(int, KeyEvent) function on line 41. We just create a switch with the KeyCode that was pressed. If a relavent key is pressed we initiate an animation with the handleAnimation(View) function (line 69) and then send the appropriate command to the service.

You can see on line 70 that it's very easy to start playing an animation on a view. You just call it's startAnimation(Animation) function passing in an Animation as it's only argument. We use AnimationUtils to get the animation from it's resource id (R.anim.selected).

Now, all that is left is to launch this new Activity when a user selects a song, here is the new onListItemClick() from the MusicDroid ListView:

  1. @Override
  2. protected void onListItemClick(ListView l, View v, int position, long id) {
  3.         try {
  4.                 Intent intent = new Intent(this, ControlsMenu.class);
  5.                 startSubActivity(intent,0);
  6.                 mpInterface.playFile(position);
  7.         } catch(DeadObjectException e) {
  8.                 Log.e(getString(R.string.app_name), e.getMessage());
  9.         }
  10. }

So playing with these new controls you can see that they are not ideal, because the controls have no idea what's going part 4 we will attempt to change that. We will also add ID3 support so that we can also display the Artist/Song information, and a progress indicator on the song check back!