0

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();
        }
    });

   }
double-beep
  • 5,031
  • 17
  • 33
  • 41
Sally Gomez
  • 554
  • 1
  • 4
  • 9
  • 1. In first activity you have a line `Songs song = songslist.get(position);` to get a song but trying to send original list with `anotherActivityIntent.putExtra("songs",songslist);` 2. In playing activity you have `ArrayList songs = ...` but seems no where to use `songs`. Which do you want to send (list of songs) or (a single song)? – Toris Dec 24 '17 at 15:47
  • ...what i want to do is whenever the user is click on any song in the list view , it shows another activity and play the chosen song – Sally Gomez Dec 24 '17 at 15:56

2 Answers2

1

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.

Toris
  • 2,348
  • 13
  • 16
0

try to change

 anotherActivityIntent.putExtra("songs",songslist);

To

 anotherActivityIntent.putParcelableArrayListExtra("songs",songslist);

Also change key name you use wrong songs & Songs make same for both side

ArrayList<Songs> songs = getIntent().getParcelableArrayListExtra("songs");                    

You also need to cast mMediaPlayer object before onclick lik this

mMediaPlayer = (MediaPlayer)findViewById(You media player id);
Munir
  • 2,548
  • 1
  • 11
  • 20
  • I changed it and it still stopped when i click on the button : '( – Sally Gomez Dec 24 '17 at 15:51
  • and why you have extend `FunActivity` in `playingActivity` ? extend `AppCompatActivity` – Munir Dec 24 '17 at 16:11
  • 12-24 11:11:09.251 3651-3651/com.example.mivida.funkids E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.mivida.funkids, PID: 3651 no errors in the code but this is what happened when i click on the button it stops the app – Sally Gomez Dec 24 '17 at 16:16
  • you not cast your media player object cast it and then use – Munir Dec 24 '17 at 16:18
  • see my updted answer – Munir Dec 24 '17 at 16:20
  • I have done what you've mentioned, mMediaPlayer = new MediaPlayer(); this is what i get now when i click on the button at least it doesn't stops and crash for now but it plays nothing 12-24 11:20:43.916 4174-4174/com.example.mivida.funkids E/MediaPlayerNative: start called in state 1, mPlayer(0x0) – Sally Gomez Dec 24 '17 at 16:22
  • you need to set data source and create it and all find some demo on internet regarding it – Munir Dec 24 '17 at 16:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161903/discussion-between-sally-gomez-and-munir). – Sally Gomez Dec 24 '17 at 16:56