get All Songs from Context - Android android.content

Android examples for android.content:Context

Description

get All Songs from Context

Demo Code

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;


public class Main {

  public static ArrayList<Song> songList;

  public static ArrayList<Song> getAllSongs(Context context) {
    ArrayList<Song> songs;/*from w  w  w  . j a v  a  2  s. c  om*/
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID },
        MediaStore.Audio.Media.MIME_TYPE + "=? or " + MediaStore.Audio.Media.MIME_TYPE + "=?",
        new String[] { "audio/mpeg", "audio/x-ms-wma" }, null);
    songs = new ArrayList<>();
    if (cursor.moveToFirst()) {
      Song song;
      do {
        song = new Song();
        song.setId(cursor.getInt(0));
        song.setFileName(cursor.getString(1));
        song.setTitle(cursor.getString(2));
        song.setDuration(cursor.getInt(3));
        song.setSinger(cursor.getString(4));
        song.setAlbum(cursor.getString(5));
        if (cursor.getString(6) != null) {
          float size = cursor.getInt(6) / 1024f / 1024f;
          song.setSize((size + "").substring(0, 4) + "M");
        } else {
          song.setSize("unknown");
        }
        if (cursor.getString(7) != null) {
          song.setFileUrl(cursor.getString(7));
        }
        song.setAlbum_id(cursor.getInt(8));
        songs.add(song);
      } while (cursor.moveToNext());
      cursor.close();
    }
    songList = songs;
    return songs;
  }

}
class Song {
  int id;
  String fileName;
  String title;
  int duration;
  String singer;
  String album;
  String size;
  String fileUrl;
  int album_id;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFileName() {
    return fileName;
  }
  public void setFileName(String fileName) {
    this.fileName = fileName;
  }
  public String getTitle() {
    return title;
  }
  public void setTitle(String title) {
    this.title = title;
  }
  public int getDuration() {
    return duration;
  }
  public void setDuration(int duration) {
    this.duration = duration;
  }
  public String getSinger() {
    return singer;
  }
  public void setSinger(String singer) {
    this.singer = singer;
  }
  public String getAlbum() {
    return album;
  }
  public void setAlbum(String album) {
    this.album = album;
  }
  public String getSize() {
    return size;
  }
  public void setSize(String size) {
    this.size = size;
  }
  public String getFileUrl() {
    return fileUrl;
  }
  public void setFileUrl(String fileUrl) {
    this.fileUrl = fileUrl;
  }
  public int getAlbum_id() {
    return album_id;
  }
  public void setAlbum_id(int album_id) {
    this.album_id = album_id;
  }
  
}

Related Tutorials