79718087

Date: 2025-07-29 04:06:24
Score: 0.5
Natty:
Report link

Perfect! Let's move forward with all of it. Here's what I’ll provide now based on your direction:


1. Add OnItemClickListener in 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);
});

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

3. Create 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>

4. Add MP3 Files to /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).


✅ Final Checklist:


📦 Want the ZIP Project?

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:

Ready when you are!

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Rp song