0
public class PlayerScreen extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

private MediaPlayer mp;
private Button buttonplaypause;
private int count;
private SeekBar seekbar;
private int curpos, prevpos;
private Bundle b;
private String title;
private final Handler handler = new Handler();
private Runnable updatePositionRunnable = new Runnable() {
    public void run() {

        seekbar.setProgress(mp.getCurrentPosition());
        handler.postDelayed(this, 6000);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player_screen);
    title = b.getString("title");
    mp = new MediaPlayer(); 
    try {
        Log.d("message", "message");
        mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Music/"+title);
        mp.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //mp = MediaPlayer.create(this, R.raw.audio);

    buttonplaypause = (Button) findViewById(R.id.buttonPlayPause);
    seekbar = (SeekBar) findViewById(R.id.seekBar);
    seekbar.setMax(mp.getDuration());
    buttonplaypause.setBackgroundResource(R.drawable.play);

    seekbar.setOnSeekBarChangeListener(this);
    handler.postDelayed(updatePositionRunnable, 6000);
}

public void onProgressChanged(SeekBar mySeekBar, int progress, boolean fromUser) {
    if (mp.isPlaying()) {
        mp.seekTo(progress);
    }
}

public void onStartTrackingTouch(SeekBar mySeekBar) {
}

public void onStopTrackingTouch(SeekBar mySeekBar) {
}

public void toPlayPause(View v) {
    if (count % 2 == 0) {
        buttonplaypause.setBackgroundResource(R.drawable.pause);
        curpos = mp.getCurrentPosition();
        prevpos = curpos;
        mp.seekTo(curpos);
        mp.start();
        count++;
        buttonplaypause.setBackgroundResource(R.drawable.play);
        mp.pause();
        count++;
    }
}
}

What i'm trying to do is build a music player which displays the list of song in one activity in a List View and when the user taps on an item that particular song should be played in another intent (which is provided by play and pause button). The code provided above is the code for the second activity (the first part i.e. listing the songs is done). The first Activity passes the song title in a bundle which is received by this activity. I am new to android and don't know a lot about it. I don't know how to pass the song from one intent to another so that it is played. The problem I think is in the line setDataSource().

When the second Intent (i.e the code provided above) is opened on tapping item in the List View, the seek bar reaches it's end and no song is played

If there is any other, better way to pass the song from one intent to another feel free to describe it in detail.

  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Dalija Prasnikar Aug 10 '16 at 18:09
  • You haven't initialized `mp` object instance – Dalija Prasnikar Aug 10 '16 at 18:10
  • Just add `mp = new MediaPlayer()` before calling `mp.setDataSource()`. – Shaishav Aug 10 '16 at 18:11
  • Thanks! It Worked. But now that this problem has been solved and the second intent is opened on tapping the song, but the song is not playing. The seekbar is automatically reaches it's end and nothing happens. – Prakhar Singh Aug 10 '16 at 18:20
  • To put it simply how should I pass the same song selected in the previous intent to the provided activity so that it can be played. – Prakhar Singh Aug 10 '16 at 18:20

0 Answers0