Perfect! Let's move forward with all of it. Here's what I’ll provide now based on your direction:
MainActivity.java
Insert this at the end of onCreate()
:
songListView.setOnItemClickListener((parent, view, position, id) -> {
String song = (String) parent.getItemAtPosition(position);
Intent intent = new Intent(MainActivity.this, PlaySongActivity.class);
intent.putExtra("songTitle", song);
startActivity(intent);
});
PlaySongActivity.java
package com.example.rpsong;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
public class PlaySongActivity extends AppCompatActivity {
TextView songTitleText;
Button btnPlayPause;
MediaPlayer mediaPlayer;
boolean isPlaying = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_song);
songTitleText = findViewById(R.id.songTitleText);
btnPlayPause = findViewById(R.id.btnPlayPause);
String songTitle = getIntent().getStringExtra("songTitle");
songTitleText.setText(songTitle);
int resId = getSongResourceId(songTitle); // Match song title to R.raw.<file>
if (resId != 0) {
mediaPlayer = MediaPlayer.create(this, resId);
} else {
Toast.makeText(this, "Audio file not found", Toast.LENGTH_SHORT).show();
}
btnPlayPause.setOnClickListener(v -> {
if (mediaPlayer == null) return;
if (isPlaying) {
mediaPlayer.pause();
btnPlayPause.setText("Play");
} else {
mediaPlayer.start();
btnPlayPause.setText("Pause");
}
isPlaying = !isPlaying;
});
}
private int getSongResourceId(String songTitle) {
songTitle = songTitle.toLowerCase().replace(" ", "_"); // "Tum Hi Ho" → "tum_hi_ho"
return getResources().getIdentifier(songTitle, "raw", getPackageName());
}
@Override
protected void onDestroy() {
if (mediaPlayer != null) {
mediaPlayer.release();
}
super.onDestroy();
}
}
activity_play_song.xml
Create this file in res/layout/activity_play_song.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="24dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/songTitleText"
android:text="Now Playing"
android:textSize="22sp"
android:layout_marginBottom="24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnPlayPause"
android:text="Play"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
/res/raw
Place your MP3 files in app/src/main/res/raw/
and name them like this:
Song TitleFile Name (in raw/)Tum Hi Hotum_hi_ho.mp3Kesariyakesariya.mp3Perfectperfect.mp3Shape of Youshape_of_you.mp3
File names must be lowercase and underscored (no spaces/symbols).
[x] Spinner works with SharedPreferences ✅
[x] Song list updates by country ✅
[x] Tapping a song opens PlaySongActivity
✅
[x] Playback with MediaPlayer
✅
If you want the full Android Studio project with this code, I can generate it as a .zip
– just confirm and I’ll prepare it.
Let me know if you'd like to:
Add a SeekBar 🎚️
Add Premium 🔒 icons for special songs
Add user profile photo/settings logic
Ready when you are!