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());
}
}