The foolowing example used to list all the music files stored in SDcard and you can play the music
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;
}
}
}
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;
}
}
}
very good .... thanks
ReplyDeletenot work for me
ReplyDeleteGood job mate, useful code
ReplyDeleteI tried building a music app using this code but it keep on giving me error message. Any help
ReplyDeletehow to dispaly the Artist Name below song name
ReplyDeletethumbline
Deleteare there any permissions required in the manifest?
ReplyDeletehow to make it with the help of Recycler View
ReplyDeleteNice Post.
ReplyDeleteFor best music samples and loops
on Indian Flutes visit mangoloops
These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful
ReplyDeleteand knowledgeable information.
Mobile App Development Company
Mobile App Development Company in India
Mobile App Development Companies
It's not working for me,when I'm trying to open on my mobile, it shows unfortunately stopped. 😭😭😭😭
ReplyDeleteTHanks it works for me
ReplyDeleteThanks u sir
ReplyDeleteThanks, worked well for me, specially the code for OnItemClick() method.
ReplyDeleteany permission required for it
ReplyDeleteMY APP DO NOT SHOW ANY ERROR. BUT ON STARTING IT STARTS AND CLOSED EMMIDIATELY
ReplyDeleteThank you so much...
ReplyDeleteThanks for sharing Information to us
ReplyDeletemobile app development company
https://z4android.blogspot.com/2012/07/how-to-check-sdcard-free-space-in.html?showComment=1602054079394#c6749768584916159611
ReplyDeleteThis comment has been removed by the author.
ReplyDeletetekirdağ
ReplyDeletetokat
elazığ
adıyaman
çankırı
KS8İ
tekirdağ
ReplyDeletetokat
elazığ
adıyaman
çankırı
G1O
bartın evden eve nakliyat
ReplyDeletebitlis evden eve nakliyat
diyarbakır evden eve nakliyat
kars evden eve nakliyat
nevşehir evden eve nakliyat
QGZ
BF545
ReplyDeleteBursa Şehir İçi Nakliyat
Ankara Boya Ustası
İzmir Evden Eve Nakliyat
Çankırı Şehir İçi Nakliyat
Bitmex Güvenilir mi
Çanakkale Şehir İçi Nakliyat
Çanakkale Lojistik
İzmir Şehirler Arası Nakliyat
Ankara Fayans Ustası
D0201
ReplyDeleteİzmir Evden Eve Nakliyat
Giresun Evden Eve Nakliyat
Hatay Parça Eşya Taşıma
Muş Şehir İçi Nakliyat
Bolu Evden Eve Nakliyat
Mexc Güvenilir mi
Samsun Lojistik
Bitexen Güvenilir mi
Adana Şehir İçi Nakliyat
9FE6C
ReplyDeleteŞırnak Parça Eşya Taşıma
Hatay Parça Eşya Taşıma
Erzurum Evden Eve Nakliyat
Kırşehir Parça Eşya Taşıma
Ünye Boya Ustası
Aksaray Evden Eve Nakliyat
Iğdır Lojistik
Çerkezköy Mutfak Dolabı
Mersin Şehir İçi Nakliyat
5D603
ReplyDeleteBingöl Şehir İçi Nakliyat
Ünye Marangoz
Ünye Fayans Ustası
Bitmart Güvenilir mi
Yalova Parça Eşya Taşıma
Bitget Güvenilir mi
Van Şehirler Arası Nakliyat
Ankara Evden Eve Nakliyat
Poloniex Güvenilir mi
0AEA8
ReplyDeleteExpanse Coin Hangi Borsada
Urfa Şehirler Arası Nakliyat
Çorum Parça Eşya Taşıma
Niğde Lojistik
Bitlis Parça Eşya Taşıma
Ort Coin Hangi Borsada
Mercatox Güvenilir mi
Tekirdağ Çatı Ustası
Kayseri Lojistik
E03E1
ReplyDeleteclenbuterol
order trenbolone enanthate
order testosterone propionat
fat burner for sale
buy testosterone propionat
order sarms
order anapolon oxymetholone
order pharmacy steroids
buy steroids
BF5BC
ReplyDeleteEryaman Parke Ustası
Diyarbakır Parça Eşya Taşıma
Maraş Parça Eşya Taşıma
Gümüşhane Evden Eve Nakliyat
Urfa Parça Eşya Taşıma
Bingöl Evden Eve Nakliyat
Mersin Şehir İçi Nakliyat
Manisa Lojistik
Etimesgut Fayans Ustası
D16D5
ReplyDeleteSinop Evden Eve Nakliyat
Siirt Parça Eşya Taşıma
Ünye Asma Tavan
Çerkezköy Halı Yıkama
Burdur Şehirler Arası Nakliyat
Kırşehir Lojistik
Siirt Şehir İçi Nakliyat
Trabzon Lojistik
Karabük Lojistik
170C8
ReplyDeletereferanskodunedir.com.tr
306E4
ReplyDeletecanli sohbet
Elazığ Canlı Sohbet Uygulamaları
ücretsiz sohbet sitesi
Afyon Sesli Sohbet Siteleri
bayburt rastgele görüntülü sohbet uygulamaları
denizli canli sohbet chat
mobil sohbet sitesi
telefonda kadınlarla sohbet
canlı görüntülü sohbet siteleri
C8FD1
ReplyDeleteerzincan sohbet sitesi
diyarbakır rastgele görüntülü sohbet ücretsiz
seslı sohbet sıtelerı
Afyon Sohbet Odaları
bedava sohbet chat odaları
niğde sohbet odaları
aksaray ücretsiz sohbet sitesi
sohbet chat
Kırşehir Görüntülü Sohbet Kızlarla
CB7FB
ReplyDeleteEskipazar
İdil
Yeşilyurt
Merzifon
Tefenni
Hafik
Çarşamba
Gölmarmara
Ortaca
شركة تنظيف سجاد بالجبيل EbanfgWhxp
ReplyDeleteشركة تنظيف افران ULeW1ZP10s
ReplyDeleteرقم مصلحة المجاري بالاحساء 9MqDdoffB6
ReplyDeleteشركة تسليك مجاري 3aaCrK3q0l
ReplyDeletehttps://thesecondhandrose.blogspot.com/2013/09/fabulous-fashionistas-six-women-that.html?sc=1732459294585#c4992189367674483401 rJmKp7jAzl
ReplyDeleteشركة عزل مواسير المياه بالخبر MY5O3Vw5HA
ReplyDeleteشركة تنظيف افران
ReplyDeleteMuwnT2402vk
شركة صيانة افران
s1UXct