in the backgroung by selecting the file from list.
main.xml
--------
<?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
< ListView
android:id="@+id/PhoneMusicList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
MusicActivity.java
-------------------
package sample.music;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class MusicActivity extends Activity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_music_grid();
}
private void init_phone_music_grid() {
System.gc();
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView> parent, View v, int position,
long id) {
System.gc();
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
};
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(mContext.getApplicationContext());
String id = null;
if (convertView == null) {
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
musiccursor.moveToPosition(position);
id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}
}
This is the first tutorial I see that doesn't need debug! (except removing spaces in the xml file, one before LinearLayout and One before ListView)
ReplyDeleteReally helped me, thanks a lot!
Hi,
ReplyDeletewhen i click the list item it's not respond for me.pls help me ya.
in a similar way i want to uplopad my audio/video file to website server on clicking a file from the list.pls help
ReplyDeleteI tried this as my first Android App. It works! I get a scrollable list of my songs.
ReplyDeleteBut when I scroll up and down, the list is mixed up. I tap on an title but it playes the wrong one. Each time I go back to the top of the list, a different song is displayed as the first song.
What is wrong?
why i copy your project but error !! :(
ReplyDeleteplease help me !! thanks a lot you!!
(sorry i speak E very bad)
public View getView(int position, View convertView, ViewGroup parent) {
ReplyDeleteSystem.gc();
String id = null;
TextView tv;
if (convertView == null) {
tv = new TextView(mContext.getApplicationContext());
} else{
tv = (TextView) convertView;
}
musiccursor.moveToPosition(position);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
return tv;
}
thank you, u saved the day for me............:)
ReplyDeleteFabulous work......
Thank you for the code, i was searching this for since last 15 days.
ReplyDeleteOnce again thank you!!! it worked for me.
hi i'm trying to play sdcard songs in a service. but it is giving me null pointer exception. please help....
ReplyDeletehai, first thanks for this tutorial..
ReplyDeletewhen i tried to execute it first time iam getting result..
but second time iam getting blank screen on my emulator......
pls help me......... :P
thanks in advance.........
Nice Work....!!
ReplyDeleteThanks for this code...!!
It helped me a lot in my work...!!
The said thing about these ... codes that are being posted is, the people trying to Use them (YOU), have no faith in what your doing. In order to make a vast array of these "FREE Online CODES" to work ... yall need to try them on a ACTUAL PHONE ... and if your to scared to do so, go back to Windows OS ... lol ... code worked perfectly for me (On the Actual Phone).
ReplyDeletei want to seperate songs according to albums ..can you help...
ReplyDeletei get force close error when i run this project
ReplyDeletejust change the Adk manager
Deletei get force close error when i run this project too ,, i use an emulator not a real phone ,, is this the problem here ?
ReplyDeletewhere to copy mp3 file in my emulator any help would be greatly appreciated
ReplyDeletereally awasome codding.
ReplyDeleteyou saved my time and
my self rahul.
ReplyDeleteso thnxs brd fr providing it.!!
can u provide me the coding of hws i can get my mp3 file from SDCArd..plz
ReplyDeletesend me coding at rahul.iec44@gmail.com
i am using you're code to also play the files display.
ReplyDeletenow i am trying to get the player to skip the song to the next song.
but....it's not working :D
do you have any ideas on how to do this?
thanks
I tried this as my first Android App. It works! I get a scrollable list of my songs.
ReplyDeleteBut when I scroll up and down, the list is mixed up. I tap on an title but it playes the wrong one. Each time I go back to the top of the list, a different song is displayed as the first song.
What is wrong?
plz help me
play wrong song, not playing the selected song. pls hlp..
ReplyDeletelist get mixed while scrolling.
ReplyDeletethanks very much
ReplyDeletehaii iam getting errors...please send me the total zip file to my mail id ..pls pls ...sel.ajaykumar@gmail.com
Deletethe song does not stop..:( plz help
ReplyDeleteand can some1 post the code for listing files according to artist and albums...
22 g code te tuhada aat e g keep it up
ReplyDeleteinitially i create sample project and then two files MusicActivity.java and main.xml
ReplyDeletewhen i run sample project then nothing is display (how to make it functional)
You just have to add file in sd card not in MusicActivity.java. then change to managedQuery to getContentResolver().query. I hope it will work!
DeleteIt Works for me! I woonder how it can play in background without service. Please can you show me de lines that enable it to do that?
ReplyDeletehaii iam getting errors...please send me the total zip file to my mail id ..pls pls ...sel.ajaykumar@gmail.com
Delete