title: Why BroadcastReceiver could crash when using MediaPlayer?
body: <p>I have strange situation in my BroadcastReceiver which uses MediaPlayer. Application crashes after certain number of playing mp3 file.</p>
  
  <p>So, I have Activity</p>
  
  <pre><code>public class MyActivity extends Activity
  {
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          initButtons();
      }
  
      private void onStartButtonClicked()
      {
          Intent service = new Intent("com.ggspot.action.LAUNCH_MY_SERVICE");
          startService(service);
      }
  
      protected void onStopButtonClicked()
      {
          MyAlarm.stop(this);
      }
  
      private void initButtons()
      {
          Button startButton = (Button) findViewById(R.id.startButton);
          Button stopButton = (Button) findViewById(R.id.stopButton);
  
          startButton.setOnClickListener(new OnClickListener()
          {
  
              @Override
              public void onClick(View v)
              {
                  onStartButtonClicked();
              }
          });
  
          stopButton.setOnClickListener(new OnClickListener()
          {
  
              @Override
              public void onClick(View v)
              {
                  onStopButtonClicked();
              }
          });
      }
  }
  </code></pre>
  
  <p>This activity launch my service by pressing button</p>
  
  <pre><code>public class MyService extends Service
  {
      @Override
      public IBinder onBind(Intent intent)
      {
          return null;
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId)
      {
          MyAlarm alarm = new MyAlarm(getApplicationContext(), 3);
          return START_STICKY;
      }    
  }
  </code></pre>
  
  <p>Service just creates BroadcastReceiver</p>
  
  <pre><code>public class MyAlarm extends BroadcastReceiver
  {
      public MyAlarm()
      {
      }
  
      public MyAlarm(Context context, int timeoutInSeconds)
      {
          AlarmManager alarmMgr = (AlarmManager) context
                  .getSystemService(Context.ALARM_SERVICE);
          Intent intent = new Intent(context, MyAlarm.class);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                  intent, PendingIntent.FLAG_UPDATE_CURRENT);
          Calendar time = Calendar.getInstance();
          alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),
                  timeoutInSeconds * 1000, pendingIntent);
      }
  
      @Override
      public void onReceive(Context context, Intent intent)
      {
          File file = new File(Environment.getExternalStorageDirectory()
              + "/music/beep-7.mp3");
  
          Uri uri = Uri.fromFile(file);
          MediaPlayer player = MediaPlayer.create(context, uri);
          player.start();
      }
  
      public static void stop(Context context)
      {
          AlarmManager alarmMgr = (AlarmManager) context
                  .getSystemService(Context.ALARM_SERVICE);
          Intent intent = new Intent(context, MyAlarm.class);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                  intent, PendingIntent.FLAG_UPDATE_CURRENT);
          alarmMgr.cancel(pendingIntent);
      }
  }
  </code></pre>
  
  <p>File <strong>/music/beep-7.mp3</strong> is just short beep sound which last less than half second.</p>
  
  <p>My AndroidManifest:</p>
  
  <pre><code>&lt;application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name" &gt;
      &lt;activity
          android:name=".MyActivity"
          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;service
          android:name=".MyService"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="com.ggspot.action.LAUNCH_MY_SERVICE"/&gt;
          &lt;/intent-filter&gt;
      &lt;/service&gt;
  
      &lt;receiver
          android:name=".MyAlarm"&gt;
      &lt;/receiver&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p>So, I expect that after pressing Start button I would hear beeping once in three seconds. Beeping will continue even if I close application. In order to stop beeping, I should launch activity and press Stop button. <br>
  What I have now is really strange for me:</p>
  
  <ul>
  <li>after pressing Start button I hear beeping 7 times and than application crashes</li>
  <li>after pressing button "Force close" after crash I can hear beeping again 7 times. And then I suppouse service is crashed.</li>
  </ul>
  
  <p>I don't understand LogCat so well. It says:<br>
  <strong>Unable to start receiver com.ggspot.test.services.MyAlarm: java.lang.NullPointerException</strong><br>
  But I don't understand where. And why only after seventh try to play mp3 file.</p>
  
  <p><strong>Updated:</strong></p>
  
  <p>LogCat trace:</p>
  
  <pre><code>10-09 08:42:23.479: E/AndroidRuntime(12578): FATAL EXCEPTION: main
  10-09 08:42:23.479: E/AndroidRuntime(12578): java.lang.RuntimeException: Unable to start receiver com.ggspot.test.services.MyAlarm: java.lang.NullPointerException
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:1809)
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.app.ActivityThread.access$2400(ActivityThread.java:117)
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.os.Handler.dispatchMessage(Handler.java:99)
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.os.Looper.loop(Looper.java:123)
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.app.ActivityThread.main(ActivityThread.java:3687)
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at java.lang.reflect.Method.invokeNative(Native Method)
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at java.lang.reflect.Method.invoke(Method.java:507)
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at dalvik.system.NativeStart.main(Native Method)
  10-09 08:42:23.479: E/AndroidRuntime(12578): Caused by: java.lang.NullPointerException
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at com.ggspot.test.services.MyAlarm.onReceive(MyAlarm.java:58)
  10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:1798)
  10-09 08:42:23.479: E/AndroidRuntime(12578):    ... 10 more
  </code></pre>
  
  <p><strong>Updated</strong><br>
  Solution is adding CompletionListener to media player and release it after mp3 is played:</p>
  
  <pre><code>@Override
  public void onReceive(Context context, Intent intent)
  {
      File file = new File(Environment.getExternalStorageDirectory()
              + "/music/beep-7.mp3");
  
      Uri uri = Uri.fromFile(file);
      MediaPlayer player = MediaPlayer.create(context, uri);
      player.start();
  
      player.setOnCompletionListener(new OnCompletionListener()
      {
  
          @Override
          public void onCompletion(MediaPlayer mp)
          {
              mp.release();                
          }
      });
  }
  </code></pre>