MusicDroid - Audio Player Part I
Introduction
As you can imagine a popular way to load music onto a cell phone will be via removable storage, such as an SD card. In part 1 of our media player tutorial we will build a simple media player that will allow the user to select a song from the SD card and play it.
Click here if you would like to download the source for this tutorial.
Note: there is a known issue with the Emulator where the mixer will cut in and out on some systems resulting in very choppy audio, hopefully this will be addressed in the next SDK release.
Layouts
This project only consists of one Activity, a ListActivity. So, for a ListActivity we need a ListView for the actual list, and another view that will be used for each item in the list. You can get fancy, but for this example we will just use a TextView to display the name of each file.
First, here is our ListView (songlist.xml):
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.c
om/apk/res/android" - android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ListView id="@id/android:list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:drawSelectorOnTop="false"/>
- <TextView id="@id/android:empty"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="No songs found on SD Card."/>
- </LinearLayout>
Very standard ListView. The TextView entry will display when there are no items in the ListView because it's using the built-in id of "@id/android:empty".
And for each file here is the TextView to be used (song_item.xml):
- <?xml version="1.0" encoding="utf-8"?>
- <TextView id="@+id/text1" xmlns:android="http://schemas.android.c
om/apk/res/android" - android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
Again, very basic, shouldn't be anything you haven't seen before.
You may be thinking, why does the screenshot above show a black ListView, when nothing in these layouts mentions color? Well, that is determined by the "theme" in the AndroidManifest.xml. In the "application" element you can define the theme by adding "android:theme="@android:style
The ListActivity
We now must work on our ListActivity which we will call MusicDroid. Here is the declaration of this class and it's onCreate() function:
- public class MusicDroid extends ListActivity {
- private List<String> songs = new ArrayList<String>();
- private MediaPlayer mp = new MediaPlayer();
- private int currentPosition = 0;
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.songlist);
- updateSongList();
- }
First we set up some private member variables to be used by this Activity. The first one is MEDIA_PATH, and we set it to "/sdcard" because that is the location of the SD card. Next comes a List of Strings that will hold the filename for each song on the list. And of course we need a MediaPlayer object, which we call mp. The final one up there is currentPosition, which we will use to store the index of the song currently playing.
The onCreate() function is pretty basic, we set our view to be the songlist view that we created above and call the function updateSongList(), here is that function:
- public void updateSongList() {
- if (home.listFiles(new Mp3Filter()).length > 0) {
- songs.add(file.getName());
- }
- ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
- R.layout.song_item, songs);
- setListAdapter(songList);
- }
- }
Here we create a File Object called "home" which points to "/sdcard". We loop through the files returned by home.ListFiles(), adding each file to our List object "songs". Once we have this list filled we create an ArrayAdapter passing in the songs list and then set it to be our ListActivity's ListAdapter on line 47. This will populate our ListView.
You may have noticed the object above called "Mp3Filter". This is an object that implements FilenameFilter. This object is used to filter out what files should be returned, this is done by implementing the accept(File, String) function. Here is the object that we can use to filter so that listFiles() only returns MP3 files:
- return (name.endsWith(".mp3"));
- }
- }
Now we should be able to build a list of all the MP3 files in /sdcard. So now we just need to be able to select a song and play it. Fist things first, let's override onListItemClick() so we will be notified when a song is clicked on:
Pretty basic function here. We set currentPosition to hold the index of the position that was clicked on and then pass in the path of the song to playSong(String), so lets take a look at what's happening in playSong(String):
- try {
- mp.reset();
- mp.setDataSource(songPath);
- mp.prepare();
- mp.start();
- // Setup listener so next song starts automatically
- mp.setOnCompletionListener(new OnCompletionListener() {
- public void onCompletion(MediaPlayer arg0) {
- nextSong();
- }
- });
- Log.v(getString(R.string.app_name), e.getMessage());
- }
- }
The MediaPlayer object makes things really easy for us here. First we call mp.reset(), which will reset the MediaPlayer to its normal state. This is required if you were playing a song and want to change the data source. The reset() function will also stop whatever is playing, so if a song is playing and then you select another it will stop that one before starting the next song.
We then pass in the path to the song to mp.setDataSource(String) and call prepare() and start(). At this point the MediaPlayer will start playing your song.
Next job is to setup an OnCompletionListener starting on line 66. The function onCompletion(MediaPlayer) will be called when the song is over. All we do there is call the function nextSong() from our Activity. Here is nextSong():
- private void nextSong() {
- if (++currentPosition >= songs.size()) {
- // Last song, just reset currentPosition
- currentPosition = 0;
- } else {
- // Play next song
- playSong(MEDIA_PATH + songs.get(currentPosition));
- }
- }
Here we check to make sure this isn't the last song on the list, if it is we won't do anything, if not we'll play the next song using the playSong(String) function.
So that's it for the code, on the next page we'll figure out how to get this thing running...
Emulator Command Line Options
There are 2 command line options that must be set for this project to work. One to mount an SD card so that you can access it at "/sdcard" and another to enable audio.
To enable the SD card first you need to create an sd card image, to do this you need to open a command prompt in your SDK/tools folder and type the following command:
mksdcard <sizeM <file
Where <size> is the size of the SD card in MB, and this must be reasonably large as a small SD card image actually crashes the emulator. And <file> is the path to the file to create. This is the command that I used:
mksdcard 128M c:\temp\sd.<
Now your SD card image is setup, so once you have your project setup you will need to supply the 2 command line arguments. So, in Eclipse go to "Run -> Open Run Dialog". Now you may need to create a new configuration if there is not one there for your application, you can do that by double clicking on "Android Application" and supplying the Name, Project, and starting Activity on the "Android" tab. On the "Emulator" tab you need to supply 2 arguments, "-sdcard <file>" and "-useaudio".
It should look something like this:

So now that this is all setup we should be ready to run this thing right? Doh, here's what we see:

We need to add songs to the SD card for this thing to work. This is pretty easy, just open a command prompt in the SDK/tools folder once again and use the "adb push <file> <destination>" command. For example:
adb push "c:\music class="es0">\02 Paranoid Android.mp3" /sdcard
And after you push a song or many songs out there you'll be able to start using this very primitive music player.
What do you have to look forward to in the next installments?
- Creating a service for the MediaPlayer
- Adding animated controls
- ID3 Tag Support
So, be sure to check back!
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@619Apps (iPhone App Developer)'Xperia Sola' trademark hints at another possible Sony handset for the U.S. - http://t.co/atxhdfFz #iPhone #android #apps
2 hours 22 min ago -
@ayakaarchdia233 (Love)I've just received an achievement: Discriminating Shopper https://t.co/LDVOSV6I #Android #Androidgames
2 hours 22 min ago -
@DevrynBluelagon (Devryn Bluelagon)I've just received an achievement: Novice Photographer https://t.co/NpjOoveN #Android #Androidgames
2 hours 22 min ago -
@games_lma (leila marie ashley)I've just received an achievement: Persistent Shopper https://t.co/vTs6DSor #Android #Androidgames
2 hours 22 min ago -
@hawkhugh (hawkhugh)Apple's iPad3 http://t.co/T9dSUJA9 #apple #ipad3 #iphone #android
2 hours 22 min ago
Poll
Useful resources
Android Development Projects
- Android App: GPS: Form: Database: Website by danishayubb
- Jquery Mobile Project 01 by menfirst
- Simple Android App to load our Mobile Site - Hiring NOW! by steadysystems
- mobile app by ultimaterrrr
- android apps by vaneet08
- Online food shopping app (anroid) by akshaynawale
- Android eBook / reader Application by bamohriz
- Android app 50k downloads by nhcteam
- Beautiful Android Live Wallpaper by wahid2o11
- App downloads by nhcteam




Comments
دردشة سورية دردشة
دردشة سورية
دردشة لبنانية
دردشة عراقية
شات سوري
شات لبناني
دردشة سوريا
دردشة لبنان
شات سوريا
شات لبنان
دردشة السويدي
منتديات السويدي
اغاني عراقية
صور فنانين
الرياضة العراقية
شعراء العراق
نغمات عراقية
اغاني عربية
اغاني كردية
دردشة عراقية
دردشة بنات العراق
دردشة صبايا بغداد
دردشة البصره
دردشة بغداد
دردشة بغدادية
دردشة صبايا بغداد
دردشة شباب العراق
دردشة بنات العراق
دردشة الكرادة
دردشة دمشق
دردشة بيروت
دردشة حلب
دردشة حلب
دردشة عراقية
دردشة العراق
شات عراقي
جات عراقي
دردشه عراقيه
دردشة صبايا لبنان
دردشة بنات لبنان
شات صوتي | دردشة صوتية | كلام
شات صوتي
| دردشة صوتية
|
كلام
| شات كلام
|
دردشة كلام
| دردشة صوتية
|
شات صوتي
| شات
|
Chat Voice
| ahj w,jd
شات صوتي
| دردشة صوتية
|
شات صوتي
| دردشة صوتية
|
دردشه
| دردشة
|
صوتي
| صوتية
|
شات صوتي
| دردشة صوتية
|
شات صوتي
| دردشة صوتية
|
الكلام
| دردشه صوتيه
|
]v]am w,jdm
| ]v]ai w,jdi
شات صوتي
شات صوتي
شات صوتي
aglla
nice topic
شبكة اغلى
موقع نواحي - دليلك الشامل
فينك حبيبي
Motores para Persianas
The author has written an excellent article, thank you for this information. Motores para Persianas
How did you really push file to your sdcard?
You don't get a "Read only file-system" error when you try to push a file to your Emulator's sdcard?
If you did, how did you get around it? (More then just 'chmod 755 sdcard' from adb shell huh?)
Atlanta Android Developer
Thanks in advance!
Thank you - the site very
Thank you - the site very very nice
شات الحب - شات مصري - شات بنات مصر - شات صوتي - دردشة مصرية - شات مصرية - شات حب - شات - دردشة - Chat - love chat - دردشة الحب
Dietetica, Alimentacion ecologica, Alimentacion biologica
Your blog I found it very good, you have very interesting topics, thanks for the information. Dietetica, Alimentacion ecologica, Alimentacion biologica
its great
thanks for your article its really great and useful
sportbike specifications SKEMA RANGKAIAN Electronic Circuit scolarship wireless car specifications gadget and computer car first look Motorcycle Modification sexi-stars. newlaptop circuits-audio electronic projects auto girl weapons system science kids gadget bikes walpaper mobil-wallpaper MODIFIKASI MOBIL plane-wallpaper celebsexypict artist-scandal sport-cars bikespict motor-modify sexi-model laptopharga gambar-artist fast-superbike autoshowmodel harga-kamera notebook-price diamonds-collections phones-gallery camera-prices home-pic audio-gallery autocarpict living-cares homes life-style
Baterias
I found a very interesting and modern note, thanks for the article and your blog. Baterias
thank you for information حجز
thank you for information
حجز فنادق مكة
فنادق مكة
Great tutorial, I am in the
Great tutorial, I am in the process of putting all my music on my Driod, stock marketsomething I have been apprehensive to do until now.stock market today Also, I don't think this a "primitive player" mortgage calculatorlooks more than adequate!mortgage calculator
The music video is just
The music video is just something that always awaited by music lovers. Because, music videos allow us to not only hear the songs that we like, but also saw action from our favorite artists.
Reverse Cell Phone Lookup
Manually overwriiten program
Manually overwriiten program will ensure the function of Nhibernate will ensure the system will be in hibernate position whenever they have detected any intruder going to come inside the system. This will benefit more for those beginner programmers. Personalized Muscle Shirt
It’s good to see this
It’s good to see this information in your post, I was looking the same but there was not any proper resource, thanx now I Thank to the post.I really loved reading your blog.It was very well authored and easy to understand.
website design company
website design firms
register website name
The music video is just
The music video is just something that always awaited by music lovers. Because, music videos allow us to not only hear the songs that we like, but also saw action from our favorite artists.
Prostate Massage
Its been long since I have
Its been long since I have find information about the Hamlet of this edition, and apparently here available a complete and accurate information on this subject. And it really helped me. check this
I Love Android
Good Job!
Thanks
Amikom.us Tempat Belanja Hosting Murah
Amikom.us Tempat Belanja Hosting Murah "Kontes SEO Amikom.us 2011"
Amikom.us Tempat Belanja Hosting Murah
Kontes SEO "Amikom.us Tempat Belanja Hosting Murah"
i love android
thanks
Amikom.us Tempat Belanja Hosting Murah
audio player
Good point. You can buy different products such as audio player at donedeal.
Property Investment
There has been criticism that it does not go far enough, or discourages regional cultural production. And also this license has been embraced by many as a way for content creators to take control of how they choose to share their intellectual property. Baju Muslim | Property Investment
hi
Good post, keep up with this interesting work. Good to know that this topic is being covered well on this website, thanks for taking the time to discuss this. Good job! azelex 15mg tube!! vibramycin
awesome
Nicely put and executed. Regarding websites for sale this article really gets down to the nitty-gritty. When it comes to websites for sale as in your excellent post, then turnkey websites or dropship websites simply take the cake. What enables you to buy a website or an online business is the very down-to-earth information given above.
Hosting
I was very pleased to find this site.I wanted to thank you for this great read.
windows vps | forex vps
thanks
thanks
diseases
diseases,disease,diseas
General Health
Diseases,disease,diseas Disea
Diseases,disease,diseas
Disease and Treatment of Cancer
Women's Health and Diseases
what kind of treatment do you
what kind of treatment do you provide? can you explain it please
Hair Removal
Wonderful, that's exactly
Wonderful, that's exactly what I was searching for! You just saved me a lot of work.Web Design Los Angeles
i like android
nitip lapak gan !!
Amikom.us Tempat Belanja Hosting Murah
Amikom.us Tempat Belanja Hosting Murah
Amikom.us Tempat Belanja Hosting Murah
Amikom.us Tempat Belanja Hosting Murah
Fashion Trends USA
Auto Concept
Auto Show Pictures
voyance
Very good articles, you are inspiration.
Congratulations again
and good luck for the future.
voyance web
voyance audiotel
voyance immediate
meduim
meduim spirit
voyance par telephone
voyance
Nice
This was exactly what i was searching for. Have been fighting for a while to do this, thanks for have posted.
Mudanzas Madrid
SD card usually used for
SD card usually used for media storing only and could not be used as a source for media player to select and play directly from the SD Card. It requires some reader. Girls party dresses
it is very helpful for me to
it is very helpful for me to lean android
GSM Kampanya | Bilişim Teknoloji | Cep Telefonu | Turkcell Kampanyaları | Avea Kampanyaları | Vodafone Kampanyaları
In my case the application is
In my case the application is stopping unexpectedly.
GSM Kampanya | Bilişim Teknoloji | Cep Telefonu | Turkcell Kampanyaları | Avea Kampanyaları | Vodafone Kampanyaları
Open source platform indeed
Open source platform indeed give an endless option and possibilities for creative programmer to create more and more interesting applications. This one is definitely a good choice to have a media player directly from SD Card global sourcing
Nice
Great post! I'm just starting out in community management/marketing media and trying to learn how to do it well - resources like this article are incredibly helpful. As our company is based in the US, it's all a bit new to us.
Buy Methoxetamine
One other observation. Why
One other observation. Why don't we limit the terms of the people on the Supreme Court. Life time in office should not be an entitlement. I can see longer term limits for the ones who are placed, but maybe 12 year term limit should be implemented. Exotic car rental miami
Nice
Excellent post. I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.
Faustus Eberle
Nitip Lapak ! hohoho
bingung arep komen opo, nitip lapak ae yoo ...
Meriahkan pesta ulang tahun bersama GarudaFood
Nice
That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.
Coches
diseases.disease,diseas
I am developping some client server application. I am confused whether i will design the connection class as service or simple java class which takes care of http connection, sending and receiving data.
Diseases,disease,diseas
General Health
Disease and Treatment of Cancer
Women's Health and Diseases
Skin Health and Skin Diseases
Sexual Health and diseases
Now i am not exact with this, but isnt it looking for it in layout, and if so i dont have it there so maybe that is the issue
glenn beck loves android
but we should boycott him glenn beck boycott
I am extremely glad to go
I am extremely glad to go through the post on this interesting musicdroid, a audio player part I. It is a good tutorial where we will build a simple media player that will allow the user to select a song from the SD card and play it. design engineering
This site is very great for
This site is very great for android users. Especially this article, it talks about audio palyer in android system, it should be very nice for the users. iefcases/
http://www.mezzi.com/Laptop-Br
memory foam mattress
There has been criticism that it does not go far enough, or discourages regional cultural production. And also this license has been embraced by many as a way for content creators to take control of how they choose to share their intellectual property.
memory foam mattress
اي فون, ايفون 4, سعر اي فون
اي فون, ايفون 4, سعر اي فون 4, برنامج ايفون, IPhone
اي فون
ايفون 4
ايفون
سعر اي فون
برنامج ايفون
IPhone
اي فون, ايفون 4, سعر اي فون 4, برنامج ايفون, IPhone
اي فون
ايفون 4
ايفون
سعر اي فون
برنامج ايفون
IPhone
اعلانات عرب اي فون
اي فون
اخبار اي فون
برنامج اي فون
العاب اي فون
ثيمات اي فون
خلفيات اي فون
نغمات اي فون
اكسسوارات اي فون
سعر اي فون
منتدى الاي فون العام
اي فون
اي فون
اي فون
اي فون
اي فون
اي فون
Is there a new version
Is there a new version available for this audio player?
thanks
Edi | Au Pair Munka Anglia
Change voice , setting pitch , key in android
HI,
Is there any way that we change voice of recorded audio and video in android. As there is a soundpool method in android , there we change voice deep voice and chipmunk.. What if I want change recorded voice to male computer voice and female computer voice. Is there any way to implement pitch , key in Android ? .Can we set voice before recording audio ?
Is there any method to set pitch , key etc .
Cahnge voice , setting pitch , key in android
HI,
Is there any way that we change voice of recorded audio and video in android. As there is a soundpool method in android , there we change voice deep voice and chipmunk.. What if I want change recorded voice to male computer voice and female computer voice. Is there any way to implement pitch , key in Android ? .Can we set voice before recording audio ? Is there any method to set pitch , key etc
Pioneer introduced a new
Pioneer introduced a new version of its entry-level DJ system comprising of two CDJ-350-W digital media players and a DJM-350-W 2-channel mixer.This Pioneer dj system will allow users to create DJ performances with various digital music files, including MP3, AAC, WAV and AIFF files that are stored on different forms of media, including USB mass storage device class products and CD-R/RW discs.