Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to play, pause and stop the song in android?
    primarykey
    data
    text
    <p>I am making an app for Android and I have made three buttons to play, pause and stop.</p> <p>My play and pause buttons are set up, so when I click the play button, it will become invisible and the pause button will display, and vice versa.</p> <p>It works just fine when I click the play button, but after I click on the pause button, it gives me an error.</p> <p>The code is given below.</p> <pre><code> package com.mpIlango; import java.io.IOException; import java.util.ArrayList; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; public class MpIlangoActivity extends Activity implements OnCheckedChangeListener { /** Called when the activity is first created. */ MediaPlayer song1,song2,song3; int whatsong = 0; private ArrayList&lt;Integer&gt; songIds; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); RadioGroup rgMusic = (RadioGroup) findViewById(R.id.rgMusic); songIds = new ArrayList&lt;Integer&gt;(); songIds.add(R.raw.fluet); songIds.add(R.raw.airtel); songIds.add(R.raw.titanic); final Button bPlay = (Button) findViewById(R.id.bPlay); final Button bStop = (Button) findViewById(R.id.bStop); final Button bPause = (Button) findViewById(R.id.bPause); bPause.setVisibility(View.GONE); rgMusic.setOnCheckedChangeListener(this); bPlay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(song1!=null) { song1.release(); } if(song2!=null) { song2.release(); } if(song3!=null) { song3.release(); } switch (whatsong) { case 1: try { song1 = MediaPlayer.create(MpIlangoActivity.this, songIds.get(0)); song1.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } song1.start(); bPlay.setVisibility(View.GONE); bPause.setVisibility(View.VISIBLE); break; case 2: try { song2 = MediaPlayer.create(MpIlangoActivity.this, songIds.get(1)); song2.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } song2.start(); bPlay.setVisibility(View.GONE); bPause.setVisibility(View.VISIBLE); break; case 3: try { song3 = MediaPlayer.create(MpIlangoActivity.this, songIds.get(2)); song3.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } song3.start(); bPlay.setVisibility(View.GONE); bPause.setVisibility(View.VISIBLE); break; } } }); bPause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bPlay.setVisibility(View.VISIBLE); bPause.setVisibility(View.GONE); if(song1.isPlaying()){ song1.pause(); } if(song2.isPlaying()){ song2.pause(); } if(song3.isPlaying()){ song3.pause(); } } }); bStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(song1!=null){ song1.release(); } if(song2!=null){ song2.release(); } if(song3!=null){ song3.release(); } } });; } @Override public void onCheckedChanged(RadioGroup group, int rbId) { switch (rbId) { case R.id.rbMusic1: whatsong = 1; break; case R.id.rbMusic2: whatsong = 2; break; case R.id.rbMusic3: whatsong = 3; break; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload