Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save developersamuelakram/bc620cfe15ddff85bcbc750a3ea1fa52 to your computer and use it in GitHub Desktop.
Save developersamuelakram/bc620cfe15ddff85bcbc750a3ea1fa52 to your computer and use it in GitHub Desktop.
Using Index position for next or prev songs in MediaPlayer Class
Hi I have been feeling very overwhelmed and discouraged I am stuck. I want to have more than one song in my music player and add next and prev buttons. Can it be done with using an ArrayList of songs and then using the index position? Can someone write a small code snippet to modify my code below and tell me how to add prev and next button. I was hoping if everytime next is clicked index position is incremented thus causing the next element of arraylist to start.
My XML CODE :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:id="@+id/play"
android:text="Play" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:id="@+id/pause"
android:text="Pause" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:id="@+id/next"
android:text="NEXT" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:id="@+id/prev"
android:text="PREV" />
AND MY JAVA CODE:
package com.example.next;
import android.media.MediaPlayer; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button;
import java.time.chrono.MinguoChronology; import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button play = (Button) findViewById(R.id.play);
Button pause = (Button) findViewById(R.id.pause);
Button next = (Button) findViewById(R.id.next);
Button prev = (Button) findViewById(R.id.prev);
final ArrayList<SONG> songs = new ArrayList<>();
songs.add(new SONG (R.raw.goblinost));
songs.add(new SONG ( R.raw.khabboon));
songs.add(new SONG (R.raw.startover));
songs.add(new SONG (R.raw.tujhekitna));
SONG song = songs.get(position); // (I CANT GET THE POSITION ITS IN RED IN MY CODE)
mMediaPlayer = MediaPlayer.create(MainActivity.this, song.getSong());
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMediaPlayer.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMediaPlayer.pause();
}
});
}
} [you can see the error][1]
[1]: https://i.stack.imgur.com/UrE8o.png
My custom class:
package com.example.next;
public class SONG {
private int mSong;
public SONG (int song) {
mSong = song;
}
public int getSong () {
return mSong;
}
}
WHAT SHOULD I do so my next and pause button increment or decrement the index positions and play songs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment