I am making online media player app.. it works very well but I am unable to play next song when I click the next button
PLAYER CLASS
public class Player extends AppCompatActivity {
Intent i;
String sname,sid,spath;
ImageButton play;
Button nxt;
MediaPlayer mp;
ArrayList list;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
play = (ImageButton)findViewById(R.id.play_pause);
nxt = (Button) findViewById(R.id.next);
nxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// NO IDEA WHAT TO DO NOW
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp = MediaPlayer.create(Player.this,Uri.parse(spath));
if(mp.isPlaying()){
if(mp!=null){
mp.pause();
// Changing button image to play button
play.setImageResource(R.drawable.uamp_ic_play_arrow_white_48dp);
}
}else{
// Resume song
if(mp!=null){
mp.start();
// Changing button image to pause button
play.setImageResource(R.drawable.uamp_ic_pause_white_48dp);
}
}
}
});
i=getIntent();
sid=i.getStringExtra("sid");
sname=i.getStringExtra("sname");
spath=i.getStringExtra("spath");
}
@Override
public void onBackPressed() {
startActivity(new Intent(Player.this, song_list.class));
finish();
super.onBackPressed();
}
}
SONGLIST CLASS
public class song_list extends AppCompatActivity {
ListView listView;
ArrayList<pojonew> list;
String result,url;
json js;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song_list);
listView = (ListView)findViewById(R.id.songs);
url="https://heeled-pipe.000webhostapp.com/select.php";
new SongData().execute();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
pojonew p=(pojonew)list.get(position);
String sid=p.getId();
String sname=p.getName();
String spath=p.getPath();
Intent i = new Intent(song_list.this,Player.class);
i.putExtra("sid",sid);
i.putExtra("sname",sname);
i.putExtra("spath",spath);
startActivity(i);
}
});
}
SONGDATA CLASS
class SongData extends AsyncTask<Void, Void , Void>
{
@Override
protected Void doInBackground(Void... params) {
js = new json();
list = new ArrayList<>();
result = js.getdatafromurl(url);
Log.d("url",url);
try {
JSONObject jo = new JSONObject(result);
JSONArray ja= jo.getJSONArray("data");
for (int i=0;i<ja.length();i++){
JSONObject jo1 = ja.getJSONObject(i);
pojonew sp = new pojonew();
sp.setId(jo1.getString("id"));
sp.setName(jo1.getString("name"));
sp.setPath(jo1.getString("path"));
list.add(sp);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
custadapter n=new custadapter(list,song_list.this);
listView.setAdapter(n);
}
}
}
on click of the play get the position of list data item and call the method below
public ModelClass loadNext(String id) {
ModelClass nextAudio = null;
int pos = 1;
for (int index = 0; index < FileList.size(); index++) {
if (ModelClass.get(index).getFileUID().equals(id)) {
pos = index + 1;
break;
}
}
if (pos != AudioFileList.size())
nextAudio = AudioFileList.get(pos);
return nextAudio;
}
load media player has your playing in on click of list view
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.