I want to create a very simple app that shows a list of songs and when clicking on song it opens a new Activity to show the playing statue of the song, I almost created it all but when I click on play button it doesn't play.
First activity:
private Button buttonPlayStop;
private MediaPlayer mMediaPlayer;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
final ArrayList<Songs> songslist = new ArrayList<Songs>();
songslist.add(new Songs("hhhhh", "cHU CHU ", R.drawable.mimi, R.raw.hhh));
songslist.add(new Songs("hhhh", "cHU CHU ", R.drawable.jes1s, R.raw.hhh));
songslist.add(new Songs("hhhh", "cHU CHU ", R.drawable.matt, R.raw.hhh));
songslist.add(new Songs("hhhhh", "cHU CHU ", R.drawable.freind, R.raw.hhh));
songslist.add(new Songs("hhhhh", "cHU CHU ", R.drawable.joe, R.raw.hhh));
songslist.add(new Songs("hhhhh ", "cHU CHU ", R.drawable.bada, R.raw.hhh));
songslist.add(new Songs("hhhhh", "cHU CHU ", R.drawable.abby, R.raw.hhh));
final SongsAdapter adapter = new SongsAdapter(this, songslist);
final ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Songs song = songslist.get(position);
Intent anotherActivityIntent = new Intent(FunActivity.this, playingActivity.class);
anotherActivityIntent.putExtra("songs",songslist);
startActivity(anotherActivityIntent);
}
});
Playing activity:
public class playingActivity extends FunActivity {
private MediaPlayer mMediaPlayer;
private Button buttonPlayStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playing);
ArrayList<Songs> songs = getIntent().getParcelableArrayListExtra("Songs");
buttonPlayStop = (Button)findViewById(R.id.ButtonPlayStop);
buttonPlayStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mMediaPlayer.start();
}
});
}
Not tested but..
First activity:
Change
anotherActivityIntent.putExtra("songs",songslist);
to
anotherActivityIntent.putExtra("songs",song);
Playing activity:
Change
ArrayList<Songs> songs = getIntent().getParcelableArrayListExtra("Songs");
to
Songs songs = getIntent().getParcelableExtra("songs");
And you need to add some codes in Playing activity to
- Translate Songs to it's path (maybe better to have the path in Songs class)
- Set the path to mMediaPlayer (Please see MediaPlayer.setDataSource(string))
- Call mMediaPlayer.prepare()
- Then call mMediaPlayer.start()
NB: Calling mMediaPlayer.start()
in onClickListener.onClick()
which is set to buttonPlayStop in your code seems to be a typo.
Please refer to MediaPlayer.StateDiagram or MediaPlayer developer guide for farther information.