How to Pause/Stop the media player from a different activity?
<p>I have declared a media player variable in MediaPActivity activity and then want to pause or stop it in a Second activity. I have declared intent in the Second class but when i try to the mediplayer on click of btn1 in MediaPActivity, the mediaplayer in the Second activity is not paused/stopped. What is wrong with the below given piece of code ?</p>

<pre><code>   public class MediaPActivity extends Activity {
Button btn, btn1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btn = (Button) findViewById(R.id.btn);
    btn1 = (Button) findViewById(R.id.btn1);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MediaPActivity.this, Second.class);
            startActivity(i);
        }
    });
    btn1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MediaPActivity.this, Second.class);
            i.setAction(Second.ACTION_PAUSE);
            }
    });
}
</code></pre>

<p>}</p>

<p>Second Activity:</p>

<pre><code>      public class Second extends Activity {
Button btnplay, btnpause;
MediaPlayer player;
public static final String ACTION_PLAY = "com.synapse.test.PLAY";
public static final String ACTION_PAUSE = "com.synapse.test.PAUSE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    btnplay = (Button) findViewById(R.id.button1);
    btnpause = (Button) findViewById(R.id.button2);
    player = new MediaPlayer();
    player = MediaPlayer.create(Second.this, R.raw.testsong_20_sec);
    btnplay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            player.start();
        }
    });
    btnpause.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            player.pause();
        }
    });
}
@Override
protected void onNewIntent(Intent intent) {
    if(intent.getAction().equals(ACTION_PLAY)){
                player.start();
        }
    else if(intent.getAction().equals(ACTION_PAUSE)){
                player.pause();
            }
        }
</code></pre>

<p>}</p>

<pre><code>Manifest File:

  &lt;activity
        android:name=".MediaPActivity"
        android:label="@string/app_name" &gt;
        &lt;intent-filter&gt;
            &lt;action android:name="android.intent.action.MAIN" /&gt;
            &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
        &lt;/intent-filter&gt;
       &lt;/activity&gt;
        &lt;activity android:name=".Second" android:launchMode="singleTop" /&gt;
</code></pre>
