I working on a music app in which user is getting mp3 file from WebService Api. When I am selecting any song in adapter class, I am getting its selectionPositions value and sending it in next activity(PlaySong) using Intent. In next activity which is actually having a layout of music player starts playing that song.
Here is my Adapter class.
public class Albumnlistadapter extends BaseAdapter {
Context cxt;
ArrayList<Albumnlistdata>ARR;
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;
public String st,satic;
public String url;
private int selectedPosition;
private static final String TAG_MESSAGES = "messages";
public SharedPreferences sharedpreferences;
String filenameee;
public Albumnlistadapter(Context cxt,ArrayList<Albumnlistdata>ARR) {
this.cxt=cxt;
this.ARR=ARR;
}
@Override
public int getCount() {
return ARR.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v=convertView;
if(v==null)
{
v=View.inflate(cxt, R.layout.songslistitem, null);
}
TextView tvaudioName=(TextView)v.findViewById(R.id.TEXT1);
LinearLayout laySong = (LinearLayout)v.findViewById(R.id.lvsong);
tvaudioName.setText(ARR.get(position).strgeneralid);
sharedpreferences = cxt.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
satic ="http://sabakuch.org/public/uploads/tracks/";
laySong.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
filenameee = ARR.get(position).strsongfileid;
Toast.makeText(cxt, filenameee, Toast.LENGTH_SHORT).show();
String PlaySongPath =satic+filenameee;
Intent i =new Intent(cxt,Playsong.class);
i.putExtra("path", PlaySongPath);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cxt.startActivity(i);
}
});
return v;
}
}
here is my next activity in which I am playing the song.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set the layout of the Activity
setContentView(R.layout.songsplay);
PATH =getIntent().getExtras().getString("path");
NAME =getIntent().getExtras().getString("name");
mediaPlayer = MediaPlayer.create(Playsong.this, Uri.parse(PATH));
finalTime = mediaPlayer.getDuration();
//initialize views
initializeViews();
}
public void initializeViews(){
b1 = (ImageView) findViewById(R.id.backButton);
b3=(ImageView)findViewById(R.id.playButton);
b4=(ImageView)findViewById(R.id.nextButton);
if(Index==0){
b3.setVisibility(View.VISIBLE);
//b2.setVisibility(View.GONE);
}else if(Index==1){
b3.setVisibility(View.GONE);
//b2.setVisibility(View.VISIBLE);
}
//mediaPlayer = MediaPlayer.create(this, Uri.parse(PATH));
// finalTime = mediaPlayer.getDuration();
b3.setOnClickListener(new View.OnClickListener() {
@SuppressLint("NewApi") @Override
public void onClick(View v) {
Index=1;
changeimageButton();
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int)startTime;
if((temp+forwardTime)<=finalTime){
startTime = startTime + forwardTime;
mediaPlayer.seekTo((int) startTime);
Toast.makeText(getApplicationContext(),"You have Jumped forward 5 seconds",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(),"Cannot jump forward 5 seconds",Toast.LENGTH_SHORT).show();
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int)startTime;
if((temp-backwardTime)>0){
startTime = startTime - backwardTime;
mediaPlayer.seekTo((int) startTime);
Toast.makeText(getApplicationContext(),"You have Jumped backward 5 seconds",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(),"Cannot jump backward 5 seconds",Toast.LENGTH_SHORT).show();
}
}
});
}
protected void changeimageButton() {
if(Index==0){
b3.setVisibility(View.VISIBLE);
}else if(Index==1){
b3.setVisibility(View.GONE);
}
}
private Runnable UpdateSongTime = new Runnable() {
public void run() {
startTime = mediaPlayer.getCurrentPosition();
myHandler.postDelayed(this, 100);
}
};
@Override
public void onBackPressed ()
{
if (mediaPlayer != null)
mediaPlayer.stop();
super.onBackPressed();
}
@Override
public void onPause ()
{
if (mediaPlayer != null)
{
mediaPlayer.pause();
mediaPlayer.stop();
}
super.onPause();
}
}
I am not having issue till now, But when I want to change the song. I guess if could be able to change the selectedPosition in previous adapter class, I will be able to get the next and previous song from the api. How can I do that?? please help me. Thanks in advance.