MusicDroid - Audio Player Part III


SDK Version: 
M3

Introduction

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"?>
  2.  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent">
  6.  
  7.         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  8.             android:layout_width="170dip"
  9.             android:layout_height="170dip"
  10.             android:layout_centerVertical="true"
  11.             android:layout_centerHorizontal="true">
  12.        
  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"; />
  19.        
  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"; />
  26.                
  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"; />
  33.        
  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" />
  41.  
  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

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="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" android:fillAfter="true">
  3.  
  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.                 />
  14.        
  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>
  3.  
  4.         <style name="Theme" parent="android:Theme.Dark"></style>
  5.  
  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 {
  2.  
  3.         private ImageView pauseImage;
  4.         private ImageView skipbImage;
  5.         private ImageView skipfImage;
  6.         private ImageView stopImage;
  7.         private MDSInterface mpInterface;
  8.  
  9.         @Override
  10.         public void onCreate(Bundle icicle) {
  11.                 super.onCreate(icicle);
  12.  
  13.                 setContentView(R.layout.controls);
  14.  
  15.                 pauseImage = (ImageView) findViewById(R.id.pause);
  16.                 skipbImage = (ImageView) findViewById(R.id.skipb);
  17.                 skipfImage = (ImageView) findViewById(R.id.skipf);
  18.                 stopImage = (ImageView) findViewById(R.id.stop);
  19.  
  20.                 this.bindService(new Intent(this, MDService.class), null, mConnection,
  21.                                 Context.BIND_AUTO_CREATE);
  22.         }
  23.  
  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.                         }
  45.  
  46.                 } catch (DeadObjectException e) {
  47.                         Log.e(getString(R.string.app_name), e.getMessage());
  48.                 }
  49.  
  50.                 return super.onKeyUp(keyCode, event);
  51.         }
  52.  
  53.         public void handleAnimation(View v) {
  54.                 v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.selected));
  55.         }
  56.  
  57.         private ServiceConnection mConnection = new ServiceConnection() {
  58.                 public void onServiceConnected(ComponentName className, IBinder service) {
  59.                         mpInterface = MDSInterface.Stub.asInterface((IBinder) service);
  60.                 }
  61.  
  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 on...in 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 playing....so check back!

Comments

شات
دردشة
شات الاسكندرية
دردشة الاسكندرية
شات اليكس
اليكس
سمعنا اغانى شعبى
استماع اغانى شعبى

تحميل اغانى شعبى
شات
دردشة
شات بنات
شات بنات مصر
دردشة بنات مصر
شات بنات مصرية
شات مصرية
شات مصرى

شات مصر
شات مصريات
شات 12
شات احلى بنات
شات بنت السعوديه
شات احمر شفايف
احسن شات
افلام بنات
اجمل شات

ازياء بنات مصر
دليل بنات مصر
دليل شات بنات مصر
رقص بنات مصر
فيس بوك بنات مصر
فضائح بنات مصر
موضة بنات مصر
جميلات بنات مصر
شات همس

شات همس
مكياج بنات مصر
شات نهر
شات نهر
شات بنت فلسطين
شات بنت فلسطين
شات بنت السعودية
شات قمر
اقوى شات

منتديات بنات مصر
منتديات بنات مصر
شات سكر بنات
شات سكر بنات
شات ياهو
شات ياهو
شات يوتيوب
دردشة يوتيوب
شات بنت مصر

دردشة بنت مصر
شات موجه
شات قلبى
شات قلوب
شات لبنان
شات احساس حب
شات دلع نجد
شات فلسطين

شات السعودية
شات بنات
دردشة بنات
شات بنت
دردشة بنت
شات تعب قلبى
شات بنت جده
شات بنت الخليج
شات بنت تونس

شات بنت العرب
شات بنت لبنان
شات بنت الرياض
شات بنت الشرقية
شات بنت نار
شات بنت دلع
شات بنت روشة
شات بنت موزة
شات امطار الحب

شات عسل
شات حبتنى
شات بنات موزز
شات الفرعون العاشق
شات موزز
شات حلمك
دردشة حلمك
شات حلمك
دردشة حلمك

شات حلا
شات حلا
شات بنات عز
شات بنت عز
شات الود
شات امير الصمت
شات مصرية
شات بنت 18
شات بحبك

دردشة بحبك
شات بحبك
دردشة بحبك
شات حبك
شات حبى
شات الحب
دردشة الحب
شات الحب
دردشة الحب

شات الشلة
دردشة الشلة
شات اصحاب كول
دردشة اصحاب كول
شات حبنا
شات بنات مصر الكتابى
شات بنت مصر الكتابى
شات مصريات
دردشة مصريات

شات بنات كول
شات اسكندرية
دردشة اسكندرية
دردشة الاسكندرية
شات كتابى
دردشةكتابية
chat
شات مصرية
دردشة بنات مصر
منتدى بنات مصر العام
منتديات بنات مصر النسائية
منتديات بنات مصر العامه
شات بنات مصر الخلاصة
منتديات بنات مصر الاسلامية
منتديات بنات مصر التقنية
منتديات بنات مصر الترفيهية
منتديات بنات مصر الادبية والصحية
بنات مصر الرياضية والسياحية
العاب بنات مصر
كليبات بنات مصر
كليبات بنات مصر
العاب بنات مصر
مركز رفع الصورر
دردشة بنات مصر
شات ايجي جيرليز
رقص بنات مصر
شات بنات الاسكندرية
شات بنات اسوان
شات بنات اسيوط
شات بنات البحيرة
شات بنات بنى سويف
شات بنات القاهرة
شات بنات المنصورة
شات بنات دمياط
شات بنات لفيوم
شات بنات طنطا
شات بنات الجيزة
شات بنات الاسماعيلية
شات كفر الشيخ
شات بنات مطروح
شات بنات المنيا
شات بنات المنوفية
شات الوادي الجديد
شات بنات العريش
شات بنات بورسعيد
شات بنات بنها
شات بنات قنا
شات بنات الغردقة
شات بنات الزقازيق
شات بنات سوهاج
شات بنات سيناء
شات بنات السويس
شات بنات الاقصر
شات بنات حلوان
شات بنات أكتوبر
شات مصراوى
وظائف خالية
اصدقاء جدد
شات بنات مصر
شات بنت مصر
دردشة بنات مصر
شات بنات مصر
شات
شات
افلام اجنبى
كليبات
فيديو كليب
افلام عربى
شات مصرى
شات مصرية
شات
شات اسكندرية
شات الاسكندرية
شات مصرى
شات مصرية
شات بنات مصر
شات بنات
شات بنات
شات بنات
شات بنات
رقص للكبار فقط
رقص
رقص بنات
شات بنات
شات بنات
شات بنات
شات بنات
شات بنات
شات بنات
شات بنات
شات بنات
شات بنات


نصرت البدر

حسام الرسام
جي فاير
كاظم الساهر
محمد السالم
فضائح الفنانين
فضائح ممثلات عاريات
اغتصاب
فضائح المشاهير
تحميل الاغاني العراقية
اغاني عراقية
ابراج الحظ,برجك اليوم
صور الفنانات,صور الممثلين
نكت عراقية
التعارف والاصدقاء
مسلسلات
صور ممثلات
صور فنانين
ابراج اليوم
الدليل العراقي
اغاني عراقية
دليل المواقع العراقية
جات عراقي
دردشة عراقية
شات العراق
دردشة العراق
اغاني عراقية mp3
اغاني mp3
عراق اب
اغاني عراقية
منتديات عراقية
مناقصات
منتدى المرأة العراقية
صور واخبار الفنانات والفنانين
دليل المواقع العراقية
منتدى العراق
منتدى بنات العراق
شات العراق
العاب كومبيوتر
وظائف شاغرة
شعر شعبي عراقي
هكر واختراق
جات كردي
دردشة بغداد
جات بغداد
دردشة الانبار
دردشة البصرة
دردشة الموصل
دردشة الحلة بابل
دردشة ديالى بعقوبة
دردشة ميسان العمارة
دردشة الناصرية
دردشة اربيل هولير
دردشة دهوك
دردشة تكريت صلاح الدين
دردشة السماوة
دردشة النجف
دردشة كربلاء
دردشة كركوك
دردشة ذي قار
دردشة العاشق
دردشة عراقية
دردشة عراق3
دردشة موسوعة الخليج
عراق الرومانسية
دردشة شط العرب
دردشة دجلةدردشة
شات عراقي
جات عراقي
موقع العاشق
دردشة العاشق
ابراج
نكت عراقية
منتديات عراقية
مناقصات
منتدى المرأة العراقية
صور واخبار الفنانات والفنانين
دليل المواقع العراقية
منتدى العراق
منتدى بنات العراق
شات العراق
العاب كومبيوتر
وظائف شاغرة
شعر شعبي عراقي
هكر واختراق
دردشة كويتية
دردشة الكويت
دردشة الصبايا الكويتية
شات كويتي
جات كردي
دردشة بغداد
جات بغداد
دردشة الانبار
دردشة البصرة
دردشة الموصل
دردشة الحلة بابل
دردشة ديالى بعقوبة
دردشة ميسان العمارة
دردشة الناصرية
دردشة اربيل هولير
دردشة دهوك
دردشة تكريت صلاح الدين
دردشة السماوة
دردشة النجف
دردشة كربلاء
دردشة كركوك
الدردشات العراقية
دردشة ذي قار

دردشة كويتية

دردشة كويتية
كويتية
دردشة الكويت
دردشة الصبايا الكويتية
شات كويتي
دردشة كويت 777
شات كويت 777
الكويت 777
موقع الكويت 777
موقع الكويت25
دردشة كويت 25
شات كويت 25
دردشة سعودية
صبايا العراق
دردشة صبايا العراق
دردشة بنوتة سعودية
دردشة السعودية
دردشة المدينة
دردشة تبوك
دردشة جيزان
دردشة نجران
دردشة القطيف
دردشة القصيم
دردشة الجوف
دردشة الحائل
دردشة الباحة
دردشة عسير
دردشة الشرقية
دردشة مكة
دردشة الرياض
دردشة عراقنا
عراقنا
شات عراقنا
موقع عراقنا
جات عراقنا
شات العراقا

دردشة عراقية

I've test other programs like musicdroid but no ones works like this program. It's really good. Test it! Muebles

Can u show us how to read the mp3 tags as well?Lake Travis Foreclosures

Thanx for sharing all these wonderful Posts and Blog.I really like them and looking forward for the new posts. Download free Latest Mp3 Songs and LISTEN Mp3 Songs Online

we desire for an excellent work from you and there is no doubt that this is a good work done by you. thanks to you for your great effort on this eczema

His lawyer pedicled with kelifudun give me a real copy of the original autopsy report, and I consider the medical evidence to support his statements.vps

A mighty cry for vengeance went up, and without waiting for further orders they charged forth in a body and made straight for the enemy. gift baskets

That's very interesting I got to read. Thank you for sharing I have bookmarked this page will back soon to read out more here. Logo Design

you leave and take your Lonely Planet guide with you. Don’t forget to learn some French before hard anodized cookware

thank you for information

حجز فنادق مكة
فنادق مكة

After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.
myegy
ماى ايجى
ماي ايجي
my egy

I should say that you have done a great job and your writing style is awesome. I was searching for this topic and just found your site when I was googling. Your blog can be much better if you put some pictures in it.
win money online

I should say that you have done a great job and your writing style is awesome. I was
searching for this topic and just found your site when I was googling. Your blog can be
much better if you put some pictures in it.
Bare Lifts

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.
Audi A5

designer so the controls might be a little bland for now, but it demonstrates how to control layouts using RelativeLayouts and ImageViews
Audi A5">Audi A5

What a wonderful piece of information Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often.Metal Stud

Wow, nice post,there are many person searching about that now they will find enough resources by your post.Thank you for sharing to us.Please one more post about that..
amazon affiliate script | amazon associate | amazon affiliate

I like the part 3 of the audio. We're able to play some good music you know. I tested it..works good.

GSM Kampanya | Bilişim Teknoloji | Cep Telefonu | Turkcell Kampanyaları | Avea Kampanyaları | Vodafone Kampanyaları

Thaks a lot, this kind of interface allows to control the music. It also demonstrates how to create a transparent activity. Very useful for my Phone.
It's a pleasure to see android's blogs with this level of info.

Audi

I am interested and looking for the attribute which demonstrates how to control layouts using relativelayouts and imageviews, as well as animating the image views. I've enjoyed reading the post. Designer hand bags online

The video for xxxo references several online social networking sites and resembles online gifts and animations that are popular on arabic language Internet forums, blogs, and social dating websites.Thanks for sharing the informative post.Austin Rental Austin Real Estate

We're able to play some good music I've tested it..works good. Thanks, man.

Need For Speed SHIFT 2
SHIFT 2 Unleashed

Baguio is certainly reputed towards the local lumber designs and carvings plus weaved systems popular perhaps abroad simply because souvenir goods. Guests goes toward Baguio merely to choose these materials, and the majority owners nevertheless utilize stiched gadgets, the industry testament of your products' durability.

Interesting post. Thanks for sharing this information.

Logo Design
Brochure design
Banner design

so i finish read musicdroid audio playerpart until the last part
foto gadis smu, camera,komputer game, film, Pencairan Es Greenland

This step by step tutorial seems to exactly what I need for my application. Nowadays I work with a similar audio player ant this thread helps me to understand how it works. rca ieftin

Hi,

Thanks for all your tutorial.

Can u show us how to read the mp3 tags as well?
I would like to display the artist, song title and album name when playing the song.

| kedaiobat.co.cc
| Perlunya Web Komunitas Event Organizer
| Perlunya Web Komunitas Event Organizer

Thanks.

It's the first android audio prog i try to do.
But it don't work on my phone...(HTC Legend, android froyo)
Chef de projet Web
Création de flyers et création d'affiches
CV chef de projet Web

What a wonderful piece of information Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often. Thumbs up!
vacature
interim

versi 3 cool.. i like this music
Stop Korupsi dan Suap di Indonesia, BlogKlik, Kontes Seo and Kontes SEO

It well run thinks for tutorial.

cristal de roche

I would like download the part 2 of the audio. We're able to play some good music I've tested it..works good. Thanks, man.
Regards
ice maker

it is sometimes hard to Pandora in a world of adults Pandora Bracelets

this is a nice post thanks

my blog: justin bieber biography | how to get rid of love handles

Hi,

I have a problem here. Would appreciate some suggestion.
When the music player is playing a song, I press the back button so that it exits from the player app. If I run the player again, it will play another song.
How to avoid this?
Thanks.

Hi,

Thanks for all your tutorial.

Can u show us how to read the mp3 tags as well?
I would like to display the artist, song title and album name when playing the song.

Thanks.

I too like part 3. Being able to play music is what makes this thing so great.
Bertoia Chairs

wow its sounds cool

I have many Tips Trik Cheat about audio how it is sound, check it

You have explained in detail how to control the music by only not playing it.
personalized coffee mugs
Stress Balls
promotional tape measures
promotional products