我在使用 MediaPlayer 的 BroadcastReceiver 中遇到了奇怪的情况。播放一定数量的 mp3 文件后应用程序崩溃。
所以,我有活动
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();
}
});
}
}
此活动通过按下按钮启动我的服务
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;
}
}
服务只是创建 BroadcastReceiver
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);
}
}
文件/music/beep-7.mp3只是持续不到半秒的短哔声。
我的安卓清单:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService">
<intent-filter>
<action android:name="com.ggspot.action.LAUNCH_MY_SERVICE"/>
</intent-filter>
</service>
<receiver
android:name=".MyAlarm">
</receiver>
</application>
所以,我希望在按下开始按钮后,我会在三秒内听到一次哔哔声。即使我关闭应用程序,蜂鸣声也会继续。为了停止哔哔声,我应该启动活动并按停止按钮。
我现在拥有的对我来说真的很奇怪:
- 按下开始按钮后,我听到哔哔声 7 次,然后应用程序崩溃
- 崩溃后按下“强制关闭”按钮后,我可以再次听到哔哔声 7 次。然后我想服务崩溃了。
我不太了解LogCat。它说:
无法启动接收器 com.ggspot.test.services.MyAlarm: java.lang.NullPointerException
但我不明白在哪里。以及为什么只有在第七次尝试播放 mp3 文件之后。
更新:
LogCat 跟踪:
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
更新
的解决方案是将 CompletionListener 添加到媒体播放器并在播放 mp3 后释放它:
@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();
}
});
}