How can I call the media player when I receive a message in Android?
<p>I want to play a sound when I receive a message. I have a BroadcastReceiver, in which I call a function from another avtivity called play which plays a sound or music. My code look like this:</p>

<h3>In my main activity</h3>

<p>My Play activity:</p>

<pre><code>public class PlayActivity extends Activity
{
    private MediaPlayer mediaPlayer;

    public void play() {
        mediaPlayer = MediaPlayer.create(this, R.raw.gotye);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
    }
</code></pre>

<p>My broadcast receiver looks like:</p>

<pre><code>package org.secure.sms;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver
{
    public static final String SMS_EXTRA_NAME = "pdus";
    public static final String SMS_URI = "content://sms";

    private MediaPlayer mediaPlayer;
    private MainActivity Play;

    public void onReceive( Context context, Intent intent )
    {
        // Get SMS map from Intent
        Bundle extras = intent.getExtras();

        String messages = "";

        if ( extras != null )
        {
            // Get received SMS array
            Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );

            // Get ContentResolver object for pushing encrypted SMS to incoming folder
            ContentResolver contentResolver = context.getContentResolver();

            for ( int i = 0; i &lt; smsExtra.length; ++i )
            {
                SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);

                String body = sms.getMessageBody().toString();
                String address = sms.getOriginatingAddress();

                messages += "SMS from " + address + " :\n";
                messages += body + "\n";

                putSmsToDatabase( contentResolver, sms );
            }

            // Display SMS message
            Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
            Play.play();
        }
    }
</code></pre>

<p>I get an error message saying app unfortunately failed. I can see the toast messages, but the app is not playing the music.</p>
