简体   繁体   中英

MediaPlayer listing songs in custom listview

So I am trying to build a music player app in android where the list of songs is displayed in a custom listview. This custom listview contains (custom_list_view.xml):

  1. ImageView for album art
  2. TextView for song title
  3. TextView for song duration

I have created a java class (SongDetails.java) which sets and gets these attributes of a song.

I have created an adapter class (CustomAdapter.java) by extending ArrayAdapter class.

The class MainActivity.java initializes the CustomAdapter and the avtivity_main.xml file has a listview to display the songs

In the MainActivty.java class, under the addSongs() method, I have requested albumart. I wanted to display album art for every song in the listview. But this line throws an exception. I guess this is because number of album arts and number of songs on the device are unequal causing it to throw an exception. How can I add album art for every song in the listview

Here is the MainActivity.java class

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

private ListView songsList;
private ArrayList<String> myList = new ArrayList<String>();
private final int MY_PERMISSION_REQUEST_READ_EXTERNAL_STORAGE = 1;
private ArrayAdapter<String> arrayAdpt;
private ArrayList<String> filepath = new ArrayList<String>();
private ArrayList<String> filename = new ArrayList<String>();
private ArrayList<SongDetails> songDetailsArrayList = new ArrayList<SongDetails>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    songsList = (ListView)findViewById(R.id.songs_list_view);

    seekPermissions();
}

public void seekPermissions() {
    if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST_READ_EXTERNAL_STORAGE);
        return;
    }
    addSongs();
}


public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch(requestCode) {
        case MY_PERMISSION_REQUEST_READ_EXTERNAL_STORAGE:
        {
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                addSongs();
            }
            else {
                seekPermissions();
            }
            return;
        }
    }
}


public void addSongs() {
    String[] requestColumns = {MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Albums.ALBUM_ART,};
    Cursor cur = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, requestColumns, null, null, null);

    int name = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
    int length = cur.getColumnIndex(MediaStore.Audio.Media.DURATION);
    int path = cur.getColumnIndex(MediaStore.Audio.Media.DATA);
    int albart = cur.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
    SongDetails mySongDetails;
    cur.moveToFirst();

    while(!cur.isAfterLast()) {

        mySongDetails = new SongDetails(cur.getString(name), cur.getInt(albart), cur.getInt(length));
        mySongDetails.setSongDuration(cur.getInt(length));
        myList.add(cur.getString(name)+"\t"+cur.getInt(length));
        filepath.add(cur.getString(path));
        mySongDetails.setSongTitle(cur.getString(name));
        filename.add(cur.getString(name));
        mySongDetails.setAlbumArt(cur.getInt(albart));
        songDetailsArrayList.add(mySongDetails);
        cur.moveToNext();
    }
    songsList.setAdapter(new CustomAdapter(this, songDetailsArrayList));
}
}

The MediaStore.Media.Albums.ALBUM_ART has a return type of String. So replacing integers with strings in all the required places did the trick. And then convert this String to Bitmap image

Bitmap bm = BitmapFactory.decodeFile(String args);

For showing the images on the ImageView use

imageView.setBitmapImage(bm); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM