Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show audio files in a listview in Android?

I have some audio files in a particular directory on the SD card. I need to show their names in a listview in Android, and if I want to play or delete that particular file, can I do this through that list view? How can I do it?

public class MyPerformanceArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MyPerformanceArrayAdapter(Context context, String[] values) {
        super(context, R.layout.main, values);
        this.context = context;
        this.values = values;
    }           

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.main, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        textView.setText(values[position]);
        // Change the icon for Windows and iPhone
        String s = values[position];
        if (s.startsWith("Windows7") || s.startsWith("iPhone")
                || s.startsWith("Solaris")) {
            //imageView.setImageResource(R.drawable.no);
        } else {
            //imageView.setImageResource(R.drawable.ok);
        }    
        return rowView;
    }
}

public class SimpleListActivity extends ListActivity {
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2" };
        MyPerformanceArrayAdapter adapter = new MyPerformanceArrayAdapter(this, values);
        setListAdapter(adapter);
    }    
}

This code is a simple listview. I need to fetch their names from a particular directory to adapter class. How can I do it?

If I do any changes to that particular file, these should be reflected in the listview.

like image 253
Rahul Avatar asked Mar 20 '12 06:03

Rahul


3 Answers

public class ReadAllFilesFromPathActivity extends Activity {
    /** Called when the activity is first created. */
    private List<String> myList;
    File file;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView listView = (ListView) findViewById(R.id.mylist);
        myList = new ArrayList<String>();

        File directory = Environment.getExternalStorageDirectory();
        file = new File( directory + "/Test" );
        File list[] = file.listFiles();

        for( int i=0; i< list.length; i++)
        {
                myList.add( list[i].getName() );
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, myList);
        listView.setAdapter(adapter); //Set all the file in the list.
    }
}
like image 130
Hulk Avatar answered Nov 13 '22 13:11

Hulk


you can check use following code to show a ll Audio file in listView and click of any one you play it ..

public class AudioListActivity extends Activity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.audiolist_activity);
    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;
    }
}

}

like image 25
SAURABH_12 Avatar answered Nov 13 '22 12:11

SAURABH_12


I assume you can display those files on the list correctly.

Next, Create Menu which shows Open and Delete:

public final static int MENU_OPEN = 0;
public final static int MENU_DELETE = 1;

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

            menu.add(0, MENU_OPEN, 0, "Open");
            menu.add(0, MENU_DELETE, 1, "Delete");
}

Then, listen to onContextItemSelected for menu press:

@Override
public boolean onContextItemSelected(MenuItem item) {

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    String filename = <Your File List>.get(info.position);

    switch(item.getItemId()) {

    case MENU_OPEN:
      Intent intent = new Intent();
      intent.setAction(android.content.Intent.ACTION_VIEW);
      File file = new File(filename);
      intent.setDataAndType(Uri.fromFile(file), "<Your MIME Type>");
      startActivity(intent);
      break;
    case MENU_DELETE:
      File file = new File(filename);
      file.delete();
      break;
    }
}

Finally, to monitor the directory in real time, you should implement a FileObserver.

http://developer.android.com/reference/android/os/FileObserver.html

like image 32
Calvin Avatar answered Nov 13 '22 14:11

Calvin