1

I am new to programming with Java. I'm creating a music player that gets music from the device internal storage. I have been able to retrieve the music files successfully by U also want the artist name and song name to show in the now playing activity.

This is the code:

public class PlayerActivity extends AppCompatActivity {

ImageView back_btn;
TextView song_name, artist_name;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
static Uri uri;
int position = -1;
static ArrayList<MusicFiles> listSongs = new ArrayList<>();

@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);
    getInit();
    getIntentMethod();

    song_name.setText(listSongs.get(position).getTitle());
    artist_name.setText(listSongs.get(position).getArtist());

    back_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            startActivities(new Intent[]{intent});

        }
    });
}

//this method gets the current intent position of the song

private void getIntentMethod() {
    position = getIntent().getIntExtra("position", -1);
    listSongs = musicFiles;
    metaData(uri);

}


// fetch IDs from xml

public void getInit() {
    back_btn = findViewById(R.id.back_btn);
    song_name = findViewById(R.id.song_name);
    artist_name = findViewById(R.id.artist_name);
}

// Meta Data retrieval code //

private void metaData(Uri uri) {
    retriever = new MediaMetadataRetriever();
    retriever.setDataSource(uri.toString()); 
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • https://stackoverflow.com/questions/30005771/android-get-song-from-media-store-if-you-have-the-song-id try this, make this can help :) – Denny Mathew Oct 28 '20 at 09:07

1 Answers1

0

You could use query on database and take all info you want like this.

    public static ArrayList<Audio> loadAudio(Context context) {
    ContentResolver contentResolver = context.getContentResolver();
    ArrayList<Audio> audioList = new ArrayList<>();
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
    String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
    Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);

    if (cursor != null && cursor.getCount() > 0) {
        audioList = new ArrayList<>();
        while (cursor.moveToNext()) {

            String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
            String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            String songId = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
            String albumId = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
            String artistId = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));

            // Save to audioList
            audioList.add(new Audio(data, title, album, artist, songId, albumId, artistId));
        }
    }
    assert cursor != null;
    cursor.close();
    return audioList;
}
  • i have done this already in my main activity when i want to retrieve the songs from my internal storage. i created another activity Now Playing but i want the song name and th artist name of the current song to show in the textview i created. – SP Crisiano Oct 28 '20 at 13:43