1
public class MarathiActivity extends Activity 
{
    ListView list;

    String[] web = {
            "song name 1 ",
            "song name 2",
            "song name 3",
            "song name 4",
            "song name 5",
            "song name 6",
            "song name 7",
            "song name 8"

    } ;

    Integer[] imageId = {
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher
    };

    public  int[] resID = {
            R.raw.song1,
            R.raw.song2,
            R.raw.song3,
            R.raw.song4,
            R.raw.song5,
            R.raw.song6,
            R.raw.song7,
            R.raw.song8
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marathi);
        CustomList adapter = new
                CustomList(MarathiActivity.this, web);
        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent i = new Intent(MarathiActivity.this,MusicPlayer.class);
                startActivity(i);
               // playSong(position);

            }
        });

its marathi list of songs,

public class MusicPlayer extends Activity
{
    private MediaPlayer mediaPlayer;
    public TextView songName, duration;
    private double timeElapsed = 0, finalTime = 0;
    private int forwardTime = 2000, backwardTime = 2000;
    private Handler durationHandler = new Handler();
    private SeekBar seekbar;
    MarathiActivity ma;
    int position0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //set the layout of the Activity
        setContentView(R.layout.musicplayer);

        //initialize views
        initializeViews();
    }

    public void initializeViews(){
        songName = (TextView) findViewById(R.id.songName);

        mediaPlayer = MediaPlayer.create(this,R.raw.songname);
        finalTime = mediaPlayer.getDuration();
        duration = (TextView) findViewById(R.id.songDuration);
        seekbar = (SeekBar) findViewById(R.id.seekBar);
        songName.setText("Sample_Song.mp3");

        seekbar.setMax((int) finalTime);
        seekbar.setClickable(false);
    }

    // play mp3 song
    public void play(View view) {
        mediaPlayer.start();
        timeElapsed = mediaPlayer.getCurrentPosition();
        seekbar.setProgress((int) timeElapsed);
        durationHandler.postDelayed(updateSeekBarTime, 100);
    }

    //handler to change seekBarTime
    private Runnable updateSeekBarTime = new Runnable() {
        public void run() {
            //get current position
            timeElapsed = mediaPlayer.getCurrentPosition();
            //set seek bar progress
            seekbar.setProgress((int) timeElapsed);
            //set time remaining
            double timeRemaining = finalTime - timeElapsed;
            duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));

            //repeat yourself that again in 100 milliseconds
            durationHandler.postDelayed(this, 100);
        }
    };

    // pause mp3 song
    public void pause(View view) {
        mediaPlayer.pause();
    }

  /*  // go forward at forwardTime seconds
    public void forward(View view) {
        //check if we can go forward at forwardTime seconds before song endes
        if ((timeElapsed + forwardTime)  0) {
            timeElapsed = timeElapsed - backwardTime;

            //seek to the exact second of the track
            mediaPlayer.seekTo((int) timeElapsed);
        }
    }*/

}

i want to create the media player and play the selected the song on item click listener in media player , i am getting trouble in music player activity, how do i map the selected song name to song which i have to save in raw folder ??

Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
  • when you click a particular song on the list, what you will get ?. song name or song url ?. if you have the song name, then get that song from your resource folder and play it. else if you have the song url, fetch it and play – Rince Thomas Apr 08 '15 at 10:06
  • we dont know where your song is located. if its on device, search it with the name you get when you click the listfield. – Rince Thomas Apr 08 '15 at 10:09
  • i have stored all the song list in raw folder, when i clicked on selected item list it will go to musicplayer activity , but on play button it gave null pointer exception – Priyanka..... Apr 08 '15 at 10:16
  • Please send your logcat –  Apr 08 '15 at 10:17
  • null pointer means, your media player didnt getting the selected song. – Rince Thomas Apr 08 '15 at 10:21
  • refer this http://stackoverflow.com/questions/14505153/how-to-play-audio-file-from-raw-assets-folder-on-the-native-default-media-player – Rince Thomas Apr 08 '15 at 10:24

2 Answers2

0

I don't really understand, what you want, but try to do this:

public class MySongs {
public String name;
public int imageId;
public int songRawId;

public MySongs(String name, int imageId, int songRawId) {
    this.name = name;
    this.imageId = imageId;
    this.songRawId = songRawId;
}    

}

Then you can use a List of your songs:

List<MySongs> songs = new ArrayList<MySongs>();

In the onCreate you can add all songs, which you want to have:

songs.add(new MySongs("name1", R.mipmap.ic_launcher, R.raw.song1));
songs.add(new MySongs("name2", R.mipmap.ic_launcher, R.raw.song2));
...

After that you can put this list in your listAdapter. In the OnItemClick just do MySongs selectedSong = songs.get(position);

Then you have all informations about the selected song. You can get them with selectedSong.name as example

user3621165
  • 212
  • 4
  • 16
0

Please modify your code like below code

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent i = new Intent(MarathiActivity.this,MusicPlayer.class);
                i.putExtra("SongName", web[position]);
                i.putExtra("Song", resID[position]);
                startActivity(i);
               // playSong(position);

            }
        });

MusicPlayer.class

    String m_songName;
    int m_songRes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //set the layout of the Activity
        setContentView(R.layout.musicplayer);


        if(getIntent().getExtras()!= null)
        {
            m_songName = getIntent().getExtras().getString("SongName");
            m_songRes = getIntent().getExtras().getInt("Song");
        }

        //initialize views
        initializeViews();
    }

    public void initializeViews(){
        songName = (TextView) findViewById(R.id.songName);

        mediaPlayer = MediaPlayer.create(this,m_songRes);
        finalTime = mediaPlayer.getDuration();
        duration = (TextView) findViewById(R.id.songDuration);
        seekbar = (SeekBar) findViewById(R.id.seekBar);
        songName.setText(m_songName);

        seekbar.setMax((int) finalTime);
        seekbar.setClickable(false);
    }